-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[bolt][aarch64] simplify rodata/literal load for X86 & AArch64 #165723
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1187,7 +1187,8 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) { | |
| uint64_t NumDynamicLocalLoadsFound = 0; | ||
|
|
||
| for (BinaryBasicBlock *BB : BF.getLayout().blocks()) { | ||
| for (MCInst &Inst : *BB) { | ||
| for (auto It = BB->begin(); It != BB->end(); ++It) { | ||
| const MCInst &Inst = *It; | ||
| unsigned Opcode = Inst.getOpcode(); | ||
| const MCInstrDesc &Desc = BC.MII->get(Opcode); | ||
|
|
||
|
|
@@ -1200,7 +1201,7 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) { | |
|
|
||
| if (MIB->hasPCRelOperand(Inst)) { | ||
| // Try to find the symbol that corresponds to the PC-relative operand. | ||
| MCOperand *DispOpI = MIB->getMemOperandDisp(Inst); | ||
| MCOperand *DispOpI = MIB->getMemOperandDisp(const_cast<MCInst &>(Inst)); | ||
| assert(DispOpI != Inst.end() && "expected PC-relative displacement"); | ||
| assert(DispOpI->isExpr() && | ||
| "found PC-relative with non-symbolic displacement"); | ||
|
|
@@ -1226,28 +1227,53 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) { | |
| } | ||
|
|
||
| // Get the contents of the section containing the target address of the | ||
| // memory operand. We are only interested in read-only sections. | ||
| // memory operand. We are only interested in read-only sections for X86, | ||
| // for aarch64 the sections can be read-only or executable. | ||
| ErrorOr<BinarySection &> DataSection = | ||
| BC.getSectionForAddress(TargetAddress); | ||
| if (!DataSection || DataSection->isWritable()) | ||
| if (!DataSection) | ||
| continue; | ||
|
|
||
| if (BC.isX86() && DataSection->isWritable()) | ||
| continue; | ||
|
|
||
| if (DataSection->isText()) { | ||
| // If data is not part of a function, check if it is part of a global CI | ||
| // Do not proceed if there aren't data markers for CIs | ||
| BinaryFunction *BFTgt = | ||
| BC.getBinaryFunctionContainingAddress(TargetAddress, | ||
| /*CheckPastEnd*/ false, | ||
| /*UseMaxSize*/ true); | ||
| const bool IsInsideFunc = | ||
| BFTgt && BFTgt->isInConstantIsland(TargetAddress); | ||
|
|
||
| auto CIEndIter = BC.AddressToConstantIslandMap.end(); | ||
| auto CIIter = BC.AddressToConstantIslandMap.find(TargetAddress); | ||
| if (!IsInsideFunc && CIIter == CIEndIter) | ||
| continue; | ||
| } | ||
|
|
||
| if (BC.getRelocationAt(TargetAddress) || | ||
| BC.getDynamicRelocationAt(TargetAddress)) | ||
| continue; | ||
|
|
||
| uint32_t Offset = TargetAddress - DataSection->getAddress(); | ||
| StringRef ConstantData = DataSection->getContents(); | ||
|
|
||
| ++NumLocalLoadsFound; | ||
| if (BB->hasProfile()) | ||
| NumDynamicLocalLoadsFound += BB->getExecutionCount(); | ||
|
|
||
| if (MIB->replaceMemOperandWithImm(Inst, ConstantData, Offset)) { | ||
| ++NumLocalLoadsSimplified; | ||
| if (BB->hasProfile()) | ||
| NumDynamicLocalLoadsSimplified += BB->getExecutionCount(); | ||
| } | ||
| uint32_t Offset = TargetAddress - DataSection->getAddress(); | ||
| StringRef ConstantData = DataSection->getContents(); | ||
| const InstructionListType Instrs = | ||
| MIB->materializeConstant(Inst, ConstantData, Offset); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For x86, could we keep the original code which directly update the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to use extra wrapper to unify for all platform, replaceMemOperandWithImm is used in another optimisations passes related to X86 |
||
| if (Instrs.empty()) | ||
| continue; | ||
|
|
||
| auto IIter = BB->findInstruction(&Inst); | ||
| It = std::next(BB->replaceInstruction(IIter, Instrs), Instrs.size()); | ||
|
|
||
| ++NumLocalLoadsSimplified; | ||
| if (BB->hasProfile()) | ||
| NumDynamicLocalLoadsSimplified += BB->getExecutionCount(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1260,6 +1286,9 @@ bool SimplifyRODataLoads::simplifyRODataLoads(BinaryFunction &BF) { | |
| } | ||
|
|
||
| Error SimplifyRODataLoads::runOnFunctions(BinaryContext &BC) { | ||
| if (BC.isRISCV()) | ||
| return Error::success(); | ||
|
|
||
| for (auto &It : BC.getBinaryFunctions()) { | ||
| BinaryFunction &Function = It.second; | ||
| if (shouldOptimize(Function) && simplifyRODataLoads(Function)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2770,6 +2770,53 @@ class AArch64MCPlusBuilder : public MCPlusBuilder { | |
| return Insts; | ||
| } | ||
|
|
||
| InstructionListType materializeConstant(const MCInst &Inst, | ||
| StringRef ConstantData, | ||
| uint64_t Offset) const override { | ||
| struct InstInfo { | ||
| // Size in bytes that Inst loads from memory. | ||
| uint8_t DataSize; | ||
| // Number of instructions needed to materialize the constant. | ||
| uint8_t numInstrs; | ||
| // Opcode to use for materializing the constant. | ||
| unsigned Opcode; | ||
| }; | ||
|
|
||
| InstInfo I; | ||
| InstructionListType Insts(0); | ||
| switch (Inst.getOpcode()) { | ||
| case AArch64::LDRWl: | ||
| I = {4, 2, AArch64::MOVKWi}; | ||
| break; | ||
| case AArch64::LDRXl: | ||
| I = {8, 4, AArch64::MOVKXi}; | ||
| break; | ||
| default: | ||
| llvm_unreachable("unexpected ldr instruction"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could there be any other PC relative LDR instructions hit the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't find instructions related to byte or half wold, now only 2 instructions are handled.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we will be able to add extra logic as necessary |
||
| break; | ||
| } | ||
|
|
||
| if (ConstantData.size() - Offset < I.DataSize) | ||
| return Insts; | ||
|
|
||
| const uint64_t ImmVal = | ||
| DataExtractor(ConstantData, true, 8).getUnsigned(&Offset, I.DataSize); | ||
|
|
||
| Insts.resize(I.numInstrs); | ||
| unsigned shift = (Insts.size() - 1) * 16; | ||
| MCPhysReg Reg = Inst.getOperand(0).getReg(); | ||
| for (unsigned i = 0; i < Insts.size(); i++, shift -= 16) { | ||
| Insts[i].setOpcode(I.Opcode); | ||
| Insts[i].clear(); | ||
| Insts[i].addOperand(MCOperand::createReg(Reg)); | ||
| Insts[i].addOperand(MCOperand::createReg(Reg)); | ||
| Insts[i].addOperand(MCOperand::createImm((ImmVal >> shift) & 0xFFFF)); | ||
| Insts[i].addOperand(MCOperand::createImm(shift)); | ||
| } | ||
|
|
||
| return Insts; | ||
| } | ||
|
|
||
| std::optional<Relocation> | ||
| createRelocation(const MCFixup &Fixup, | ||
| const MCAsmBackend &MAB) const override { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // this test checks a load literal instructions changed to movk | ||
|
|
||
| # REQUIRES: system-linux | ||
|
|
||
| # RUN: rm -rf %t && split-file %s %t | ||
|
|
||
| # RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \ | ||
| # RUN: --defsym CIBIGFUNC=1 \ | ||
| # RUN: %t/materialized-ci.s -o %t/materialized-ci.o | ||
| # RUN: %clang %t/materialized-ci.o -Wl,-q \ | ||
| # RUN: -o %t/materialized-ci.exe | ||
| # RUN: llvm-bolt %t/materialized-ci.exe \ | ||
| # RUN: -o %t/materialized-ci.bolt --lite=0 \ | ||
| # RUN: --keep-nops --eliminate-unreachable=false \ | ||
| # RUN: | FileCheck %s --check-prefix=CHECK-LOGS | ||
|
|
||
| # RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \ | ||
| # RUN: --defsym CIOUTSIDEFUNC=1 \ | ||
| # RUN: %t/materialized-ci.s -o %t/materialized-ci.o | ||
| # RUN: %clang %t/materialized-ci.o -Wl,-q \ | ||
| # RUN: -o %t/materialized-ci.exe | ||
| # RUN: llvm-bolt %t/materialized-ci.exe \ | ||
| # RUN: -o %t/materialized-ci.bolt --lite=0 \ | ||
| # RUN: --keep-nops --eliminate-unreachable=false \ | ||
| # RUN: | FileCheck %s --check-prefix=CHECK-LOGS | ||
|
|
||
| # CHECK-LOGS: simplified 2 out of 2 loads | ||
|
|
||
| #--- materialized-ci.s | ||
| .text | ||
| .align 4 | ||
| .local foo | ||
| .type foo, %function | ||
| foo: | ||
| stp x29, x30, [sp, #-32]! | ||
| stp x19, x20, [sp, #16] | ||
| mov x29, sp | ||
|
|
||
| mov w19, #0 // counter = 0 | ||
| mov w22, #0 // result = 0 | ||
|
|
||
| ldr w23, .Llimit | ||
| ldr x24, .LStep | ||
|
|
||
| .ifdef CIBIGFUNC | ||
| b .LStub | ||
| .LConstants: | ||
| .Llimit: .word 100 | ||
| .LStep: .xword 3 | ||
| .LStub: | ||
| .rep 0x100000 | ||
| nop | ||
| .endr | ||
| b .Lmain_loop | ||
| .endif | ||
|
|
||
| .Lmain_loop: | ||
| madd w22, w19, w24, w22 // result += counter * increment | ||
| add w19, w19, #1 | ||
| cmp w19, w23 | ||
| b.lt .Lmain_loop | ||
| mov w0, w22 | ||
| b .Lreturn_point | ||
| .Lreturn_point: | ||
| ldp x19, x20, [sp, #16] | ||
| ldp x29, x30, [sp], #32 | ||
| ret | ||
| .size foo, .-foo | ||
|
|
||
| .ifdef CIOUTSIDEFUNC | ||
| .LConstants: | ||
| .Llimit: .word 100 | ||
| .LStep: .xword 3 | ||
| .endif | ||
|
|
||
|
|
||
| .global main | ||
| .type main, %function | ||
| main: | ||
| mov x0, #0 | ||
| bl foo | ||
| mov x0, 0 | ||
| mov w8, #93 | ||
| svc #0 | ||
|
|
||
| .size main, .-main | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.