-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[RISCV][CodeGen] Support multiple save locations for the same callee-saved register #168159
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?
Conversation
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/CodeGen/CFIInstrInserter.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/llvm/lib/CodeGen/CFIInstrInserter.cpp b/llvm/lib/CodeGen/CFIInstrInserter.cpp
index 667928304..20fe26afe 100644
--- a/llvm/lib/CodeGen/CFIInstrInserter.cpp
+++ b/llvm/lib/CodeGen/CFIInstrInserter.cpp
@@ -65,104 +65,104 @@ class CFIInstrInserter : public MachineFunctionPass {
return insertedCFI;
}
- private:
+private:
#define INVALID_REG UINT_MAX
#define INVALID_OFFSET INT_MAX
- /// contains the location where CSR register is saved.
- struct CSRSavedLocation {
- enum Kind { INVALID, REGISTER, CFA_OFFSET };
- CSRSavedLocation() {
- K = Kind::INVALID;
- Reg = 0;
- Offset = 0;
- }
- Kind K;
- // Dwarf register number
- unsigned Reg;
- // CFA offset
- int64_t Offset;
- bool isValid() const { return K != Kind::INVALID; }
- bool operator==(const CSRSavedLocation &RHS) const {
- switch (K) {
- case Kind::INVALID:
- return !RHS.isValid();
- case Kind::REGISTER:
- return Reg == RHS.Reg;
- case Kind::CFA_OFFSET:
- return Offset == RHS.Offset;
- }
- llvm_unreachable("Unknown CSRSavedLocation Kind!");
- }
- void dump(raw_ostream &OS) const {
- switch (K) {
- case Kind::INVALID:
- OS << "INVALID";
- break;
- case Kind::REGISTER:
- OS << "In Dwarf register: " << Reg;
- break;
- case Kind::CFA_OFFSET:
- OS << "At CFA offset: " << Offset;
- break;
- }
- }
- };
-
- struct MBBCFAInfo {
- MachineBasicBlock *MBB;
- /// Value of cfa offset valid at basic block entry.
- int64_t IncomingCFAOffset = -1;
- /// Value of cfa offset valid at basic block exit.
- int64_t OutgoingCFAOffset = -1;
- /// Value of cfa register valid at basic block entry.
- unsigned IncomingCFARegister = 0;
- /// Value of cfa register valid at basic block exit.
- unsigned OutgoingCFARegister = 0;
- /// Set of locations where the callee saved registers are at basic block
- /// entry.
- SmallVector<CSRSavedLocation> IncomingCSRLocations;
- /// Set of locations where the callee saved registers are at basic block
- /// exit.
- SmallVector<CSRSavedLocation> OutgoingCSRLocations;
- /// If in/out cfa offset and register values for this block have already
- /// been set or not.
- bool Processed = false;
- };
-
- /// Contains cfa offset and register values valid at entry and exit of basic
- /// blocks.
- std::vector<MBBCFAInfo> MBBVector;
-
- /// Calculate cfa offset and register values valid at entry and exit for all
- /// basic blocks in a function.
- void calculateCFAInfo(MachineFunction &MF);
- /// Calculate cfa offset and register values valid at basic block exit by
- /// checking the block for CFI instructions. Block's incoming CFA info
- /// remains the same.
- void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
- /// Update in/out cfa offset and register values for successors of the basic
- /// block.
- void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
-
- /// Check if incoming CFA information of a basic block matches outgoing CFA
- /// information of the previous block. If it doesn't, insert CFI instruction
- /// at the beginning of the block that corrects the CFA calculation rule for
- /// that block.
- bool insertCFIInstrs(MachineFunction &MF);
- /// Return the cfa offset value that should be set at the beginning of a MBB
- /// if needed. The negated value is needed when creating CFI instructions
- /// that set absolute offset.
- int64_t getCorrectCFAOffset(MachineBasicBlock *MBB) {
- return MBBVector[MBB->getNumber()].IncomingCFAOffset;
- }
-
- void reportCFAError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
- void reportCSRError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
- /// Go through each MBB in a function and check that outgoing offset and
- /// register of its predecessors match incoming offset and register of that
- /// MBB, as well as that incoming offset and register of its successors match
- /// outgoing offset and register of the MBB.
- unsigned verify(MachineFunction &MF);
+ /// contains the location where CSR register is saved.
+ struct CSRSavedLocation {
+ enum Kind { INVALID, REGISTER, CFA_OFFSET };
+ CSRSavedLocation() {
+ K = Kind::INVALID;
+ Reg = 0;
+ Offset = 0;
+ }
+ Kind K;
+ // Dwarf register number
+ unsigned Reg;
+ // CFA offset
+ int64_t Offset;
+ bool isValid() const { return K != Kind::INVALID; }
+ bool operator==(const CSRSavedLocation &RHS) const {
+ switch (K) {
+ case Kind::INVALID:
+ return !RHS.isValid();
+ case Kind::REGISTER:
+ return Reg == RHS.Reg;
+ case Kind::CFA_OFFSET:
+ return Offset == RHS.Offset;
+ }
+ llvm_unreachable("Unknown CSRSavedLocation Kind!");
+ }
+ void dump(raw_ostream &OS) const {
+ switch (K) {
+ case Kind::INVALID:
+ OS << "INVALID";
+ break;
+ case Kind::REGISTER:
+ OS << "In Dwarf register: " << Reg;
+ break;
+ case Kind::CFA_OFFSET:
+ OS << "At CFA offset: " << Offset;
+ break;
+ }
+ }
+ };
+
+ struct MBBCFAInfo {
+ MachineBasicBlock *MBB;
+ /// Value of cfa offset valid at basic block entry.
+ int64_t IncomingCFAOffset = -1;
+ /// Value of cfa offset valid at basic block exit.
+ int64_t OutgoingCFAOffset = -1;
+ /// Value of cfa register valid at basic block entry.
+ unsigned IncomingCFARegister = 0;
+ /// Value of cfa register valid at basic block exit.
+ unsigned OutgoingCFARegister = 0;
+ /// Set of locations where the callee saved registers are at basic block
+ /// entry.
+ SmallVector<CSRSavedLocation> IncomingCSRLocations;
+ /// Set of locations where the callee saved registers are at basic block
+ /// exit.
+ SmallVector<CSRSavedLocation> OutgoingCSRLocations;
+ /// If in/out cfa offset and register values for this block have already
+ /// been set or not.
+ bool Processed = false;
+ };
+
+ /// Contains cfa offset and register values valid at entry and exit of basic
+ /// blocks.
+ std::vector<MBBCFAInfo> MBBVector;
+
+ /// Calculate cfa offset and register values valid at entry and exit for all
+ /// basic blocks in a function.
+ void calculateCFAInfo(MachineFunction &MF);
+ /// Calculate cfa offset and register values valid at basic block exit by
+ /// checking the block for CFI instructions. Block's incoming CFA info
+ /// remains the same.
+ void calculateOutgoingCFAInfo(MBBCFAInfo &MBBInfo);
+ /// Update in/out cfa offset and register values for successors of the basic
+ /// block.
+ void updateSuccCFAInfo(MBBCFAInfo &MBBInfo);
+
+ /// Check if incoming CFA information of a basic block matches outgoing CFA
+ /// information of the previous block. If it doesn't, insert CFI instruction
+ /// at the beginning of the block that corrects the CFA calculation rule for
+ /// that block.
+ bool insertCFIInstrs(MachineFunction &MF);
+ /// Return the cfa offset value that should be set at the beginning of a MBB
+ /// if needed. The negated value is needed when creating CFI instructions
+ /// that set absolute offset.
+ int64_t getCorrectCFAOffset(MachineBasicBlock *MBB) {
+ return MBBVector[MBB->getNumber()].IncomingCFAOffset;
+ }
+
+ void reportCFAError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
+ void reportCSRError(const MBBCFAInfo &Pred, const MBBCFAInfo &Succ);
+ /// Go through each MBB in a function and check that outgoing offset and
+ /// register of its predecessors match incoming offset and register of that
+ /// MBB, as well as that incoming offset and register of its successors match
+ /// outgoing offset and register of the MBB.
+ unsigned verify(MachineFunction &MF);
};
} // namespace
|
Previously there was an implicit assumption in the code that a CSR can only be saved in one location. There can be scenarious where CSR is saved in different locations.
a7a8256 to
fa53125
Compare
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) LLVMLLVM.CodeGen/X86/cfi-inserter-callee-save-register-2.mirLLVM.CodeGen/X86/cfi-inserter-callee-save-register.mirLLVM.CodeGen/X86/cfi-inserter-verify-inconsistent-loc.mirLLVM.CodeGen/X86/segmented-stacks-dynamic.llLLVM.CodeGen/X86/segmented-stacks.llLLVM.DebugInfo/MIR/X86/no-cfi-loc.mirIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
Record callee-saved register locations instead of which register. Use SmallVector of
CSRSavedLocationin MBBCFAInfo indexed by the register index. Reorder the declarations to make full type of CSRSavedLocation available.