Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix backward branch veneers #120

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/aarch64/macro-assembler-aarch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -8249,9 +8249,10 @@ class MacroAssembler : public Assembler, public MacroAssemblerInterface {
UseScratchRegisterScope* scratch_scope);

bool LabelIsOutOfRange(Label* label, ImmBranchType branch_type) {
int64_t offset = label->GetLocation() - GetCursorOffset();
VIXL_ASSERT(IsMultiple(offset, kInstructionSize));
return !Instruction::IsValidImmPCOffset(branch_type,
label->GetLocation() -
GetCursorOffset());
offset / kInstructionSize);
}

void ConfigureSimulatorCPUFeaturesHelper(const CPUFeatures& features,
Expand Down
70 changes: 70 additions & 0 deletions test/aarch64/test-assembler-aarch64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13516,6 +13516,76 @@ TEST(collision_literal_veneer_pools) {
END();
}

static void VeneerBackwardBranchHelper(ImmBranchType type, int limit) {
SETUP();
START();

// This is a code generation test. The code generated is not executed.

__ Mov(x0, 1);

// Non-veneer case: generate 'limit' instructions, plus the branch itself.
Label start0;
__ Bind(&start0);
for (int i = 0; i < limit; i++) {
__ Nop();
}
switch (type) {
case CompareBranchType:
__ Cbz(x0, &start0);
break;
case TestBranchType:
__ Tbz(x0, 0, &start0);
break;
default:
VIXL_ASSERT(type == CondBranchType);
__ B(eq, &start0);
}
VIXL_CHECK(masm.GetSizeOfCodeGeneratedSince(&start0) ==
((limit + 1) * kInstructionSize));

// Veneer case: As above, plus one extra nop and a branch for the veneer; we
// expect a total of limit + 3 instructions.
//
// start1:
// nop x (limit + 1)
// tbnz skip_veneer
// b start1
// skip_veneer:
//
Label start1;
__ Bind(&start1);
for (int i = 0; i < limit; i++) {
__ Nop();
}
__ Nop(); // One extra instruction to exceed branch range.
switch (type) {
case CompareBranchType:
__ Cbz(x0, &start0);
break;
case TestBranchType:
__ Tbz(x0, 0, &start0);
break;
default:
VIXL_ASSERT(type == CondBranchType);
__ B(eq, &start0);
}
VIXL_CHECK(masm.GetSizeOfCodeGeneratedSince(&start1) ==
((limit + 3) * kInstructionSize));

END();
DISASSEMBLE();
}

TEST(veneer_backward_tbz) { VeneerBackwardBranchHelper(TestBranchType, 8192); }

TEST(veneer_backward_cbz) {
VeneerBackwardBranchHelper(CompareBranchType, 262144);
}

TEST(veneer_backward_bcond) {
VeneerBackwardBranchHelper(CondBranchType, 262144);
}

TEST(ldr_literal_explicit) {
SETUP();
Expand Down