llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Exegesis] Implemented strategy for load operation

Open AnastasiyaChernikova opened this issue 2 years ago • 1 comments

This fix helps to map operand memory to destination registers. If instruction is load, we can self-alias it in case when instruction overrides whole address register. For that we use provided scratch memory.

AnastasiyaChernikova avatar Oct 23 '24 13:10 AnastasiyaChernikova

@llvm/pr-subscribers-tools-llvm-exegesis

Author: None (AnastasiyaChernikova)

Changes

This fix helps to map operand memory to destination registers. If instruction is load, we can self-alias it in case when instruction overrides whole address register. For that we use provided scratch memory.


Full diff: https://github.com/llvm/llvm-project/pull/113458.diff

1 Files Affected:

  • (modified) llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp (+45-1)
diff --git a/llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp b/llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
index 7100b51bbb7298..ac4881f298618d 100644
--- a/llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
+++ b/llvm/tools/llvm-exegesis/lib/SerialSnippetGenerator.cpp
@@ -113,7 +113,51 @@ static void appendCodeTemplates(const LLVMState &State,
   }
   case ExecutionMode::SERIAL_VIA_MEMORY_INSTR: {
     // Select back-to-back memory instruction.
-    // TODO: Implement me.
+
+    auto &I = Variant.getInstr();
+    if (I.Description.mayLoad()) {
+      // If instruction is load, we can self-alias it in case when instruction
+      // overrides whole address register. For that we use provided scratch
+      // memory.
+
+      // TODO: now it is not checked if load writes the whole register.
+
+      auto DefOpIt = find_if(I.Operands, [](Operand const &op) {
+        return op.isDef() && op.isReg();
+      });
+
+      if (DefOpIt == I.Operands.end())
+        return;
+
+      const Operand &DefOp = *DefOpIt;
+      auto &ET = State.getExegesisTarget();
+      auto ScratchMemoryRegister = ET.getScratchMemoryRegister(
+          State.getTargetMachine().getTargetTriple());
+      auto &RegClass =
+          State.getTargetMachine().getMCRegisterInfo()->getRegClass(
+              DefOp.getExplicitOperandInfo().RegClass);
+
+      // Register classes of def operand and memory operand must be the same
+      // to perform aliasing.
+      if (!RegClass.contains(ScratchMemoryRegister))
+        return;
+
+      ET.fillMemoryOperands(Variant, ScratchMemoryRegister, 0);
+      Variant.getValueFor(DefOp) = MCOperand::createReg(ScratchMemoryRegister);
+
+      CodeTemplate CT;
+      CT.Execution = ExecutionModeBit;
+      if (CT.ScratchSpacePointerInReg == 0)
+        CT.ScratchSpacePointerInReg = ScratchMemoryRegister;
+
+      CT.Info = std::string(ExecutionClassDescription);
+      CT.Instructions.push_back(std::move(Variant));
+      CT.PreinitScratchMemory.emplace_back(ScratchMemoryRegister,
+                                           /* Offset */ 0);
+      CodeTemplates.push_back(std::move(CT));
+    }
+
+    // TODO: implement more cases
     return;
   }
   case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS: {

llvmbot avatar Oct 23 '24 13:10 llvmbot

@legrosbuffle @mshockwave @topperc @boomanaiden154 Please take a look

AnastasiyaChernikova avatar Oct 28 '24 13:10 AnastasiyaChernikova

Yes, this is part of #89047, which was asked to be discussed in this comment (https://github.com/llvm/llvm-project/pull/89047#discussion_r1731617185). This only affects boot support for riscv, it doesn't change anything on other platforms (I looked at the --opcode-index=-1 entries in the add code for all supported architectures). Since the commit with riscv support hasn't been pushed yet, I can't add tests here.

AnastasiyaChernikova avatar Nov 05 '24 15:11 AnastasiyaChernikova

Yes, this is part of #89047, which was asked to be discussed in this comment (#89047 (comment)). This only affects boot support for riscv, it doesn't change anything on other platforms (I looked at the --opcode-index=-1 entries in the add code for all supported architectures). Since the commit with riscv support hasn't been pushed yet, I can't add tests here.

Is it possible to write test with other platforms, like ARM? If not, could you add a line in the PR description that this PR is stacked on top of #89047 ?

mshockwave avatar Nov 05 '24 19:11 mshockwave

@mshockwave @legrosbuffle @topperc @boomanaiden154 I answered all the comments, please take a look

AnastasiyaChernikova avatar Jan 01 '25 22:01 AnastasiyaChernikova

@mshockwave @legrosbuffle @topperc @boomanaiden154 I look forward to further review

AnastasiyaChernikova avatar Jan 13 '25 13:01 AnastasiyaChernikova

@mshockwave @legrosbuffle @topperc @boomanaiden154 Please take a look

AnastasiyaChernikova avatar Jan 29 '25 10:01 AnastasiyaChernikova

@mshockwave, do you have any objections for this?

dybv-sc avatar Feb 04 '25 10:02 dybv-sc

It's been a month and there was no objections and this patch is LGTM, so I would say that it is good to merge. I'll wait till the end of this week though, so original reviews may take a look once more @mshockwave @legrosbuffle @topperc @boomanaiden154

dybv-sc avatar Mar 03 '25 12:03 dybv-sc