Skip to content

[NFC][TableGen] Create valid Dag in VarLenCodeEmitter #140283

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

Merged
merged 1 commit into from
May 19, 2025

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented May 16, 2025

  • Set the Dag ArgNames correctly when normalizing the Dag for slice.
  • Add unit test to exercise the "slice" hi/lo swap case.

@jurahul jurahul force-pushed the tablegen_varlenencode_bugfix branch 3 times, most recently from 67c98a0 to 3302d58 Compare May 19, 2025 15:46
@jurahul jurahul marked this pull request as ready for review May 19, 2025 17:23
@jurahul jurahul requested a review from mshockwave May 19, 2025 17:23
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes
  • Set the Dag ArgNames correctly when normalizing the Dag for slice.
  • Add unit test to exercise the "slice" hi/lo swap case.

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

2 Files Affected:

  • (modified) llvm/test/TableGen/VarLenEncoder.td (+14-6)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+4-4)
diff --git a/llvm/test/TableGen/VarLenEncoder.td b/llvm/test/TableGen/VarLenEncoder.td
index 0fabf4150b79d..95c68c44c1013 100644
--- a/llvm/test/TableGen/VarLenEncoder.td
+++ b/llvm/test/TableGen/VarLenEncoder.td
@@ -36,6 +36,8 @@ class MyVarInst<MyMemOperand memory_op> : Instruction {
     (operand "$dst", 4),
     // Testing operand referencing with a certain bit range.
     (slice "$dst", 3, 1),
+    // Testing slice ho/lo swap.
+    (slice "$dst", 1, 3),
     // Testing custom encoder
     (operand "$dst", 2, (encoder "myCustomEncoder"))
   );
@@ -57,9 +59,9 @@ def FOO16 : MyVarInst<MemOp16<"src">>;
 def FOO32 : MyVarInst<MemOp32<"src">>;
 
 // The fixed bits part
-// CHECK: {/*NumBits*/41,
+// CHECK: {/*NumBits*/44,
 // CHECK-SAME: // FOO16
-// CHECK: {/*NumBits*/57,
+// CHECK: {/*NumBits*/60,
 // CHECK-SAME: // FOO32
 // CHECK: UINT64_C(46848), // FOO16
 // CHECK: UINT64_C(46848), // FOO32
@@ -78,9 +80,12 @@ def FOO32 : MyVarInst<MemOp32<"src">>;
 // 2nd dst
 // CHECK: getMachineOpValue(MI, MI.getOperand(0), /*Pos=*/36, Scratch, Fixups, STI);
 // CHECK: Inst.insertBits(Scratch.extractBits(3, 1), 36);
+// Slice hi/low swap
+// CHECK: getMachineOpValue(MI, MI.getOperand(0), /*Pos=*/39, Scratch, Fixups, STI);
+// CHECK: Inst.insertBits(Scratch.extractBits(3, 1), 39);
 // dst w/ custom encoder
-// CHECK: myCustomEncoder(MI, /*OpIdx=*/0, /*Pos=*/39, Scratch, Fixups, STI);
-// CHECK: Inst.insertBits(Scratch.extractBits(2, 0), 39);
+// CHECK: myCustomEncoder(MI, /*OpIdx=*/0, /*Pos=*/42, Scratch, Fixups, STI);
+// CHECK: Inst.insertBits(Scratch.extractBits(2, 0), 42);
 
 // CHECK-LABEL: case ::FOO32: {
 // CHECK: Scratch.getBitWidth() < 32
@@ -96,6 +101,9 @@ def FOO32 : MyVarInst<MemOp32<"src">>;
 // 2nd dst
 // CHECK: getMachineOpValue(MI, MI.getOperand(0), /*Pos=*/52, Scratch, Fixups, STI);
 // CHECK: Inst.insertBits(Scratch.extractBits(3, 1), 52);
+// Slice hi/low swap
+// CHECK: getMachineOpValue(MI, MI.getOperand(0), /*Pos=*/55, Scratch, Fixups, STI);
+// CHECK: Inst.insertBits(Scratch.extractBits(3, 1), 55);
 // dst w/ custom encoder
-// CHECK: myCustomEncoder(MI, /*OpIdx=*/0, /*Pos=*/55, Scratch, Fixups, STI);
-// CHECK: Inst.insertBits(Scratch.extractBits(2, 0), 55);
+// CHECK: myCustomEncoder(MI, /*OpIdx=*/0, /*Pos=*/58, Scratch, Fixups, STI);
+// CHECK: Inst.insertBits(Scratch.extractBits(2, 0), 58);
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
index 1d172ab6109c1..9de6a585eb4de 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
@@ -212,10 +212,10 @@ void VarLenInst::buildRec(const DagInit *DI) {
 
     if (NeedSwap) {
       // Normalization: Hi bit should always be the second argument.
-      const Init *const NewArgs[] = {OperandName, LoBit, HiBit};
-      // TODO: This creates an invalid DagInit with 3 Args but 0 ArgNames.
-      // Extend unit test to exercise this and fix it.
-      Segments.push_back({NumBits, DagInit::get(DI->getOperator(), NewArgs, {}),
+      SmallVector<std::pair<const Init *, const StringInit *>> NewArgs(
+          DI->getArgAndNames());
+      std::swap(NewArgs[1], NewArgs[2]);
+      Segments.push_back({NumBits, DagInit::get(DI->getOperator(), NewArgs),
                           CustomEncoder, CustomDecoder});
     } else {
       Segments.push_back({NumBits, DI, CustomEncoder, CustomDecoder});

Copy link
Member

@mshockwave mshockwave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except one typo, thanks!

- Set the Dag ArgNames correctly when normalizing the Dag for slice.
@jurahul jurahul force-pushed the tablegen_varlenencode_bugfix branch from 3302d58 to 94c9328 Compare May 19, 2025 19:15
@jurahul jurahul merged commit 29fd767 into llvm:main May 19, 2025
6 of 10 checks passed
@jurahul jurahul deleted the tablegen_varlenencode_bugfix branch May 19, 2025 19:27
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
- Set the Dag ArgNames correctly when normalizing the Dag for slice.
- Add unit test to exercise the "slice" hi/lo swap case.
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
- Set the Dag ArgNames correctly when normalizing the Dag for slice.
- Add unit test to exercise the "slice" hi/lo swap case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants