[llvm] [TableGen] Fix wrong bits output in GenericTable (PR #66867)

Wang Pengcheng via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 22 03:10:50 PDT 2023


https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/66867

>From aa129897df84470b1aac115a49e92f8d7f5649f1 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Wed, 20 Sep 2023 15:04:33 +0800
Subject: [PATCH 1/2] [TableGen] Add tests to show wrong bits output in
 GenericTable

---
 llvm/test/TableGen/generic-tables.td | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/test/TableGen/generic-tables.td b/llvm/test/TableGen/generic-tables.td
index f604f7777b3df9b..2528448f732c67b 100644
--- a/llvm/test/TableGen/generic-tables.td
+++ b/llvm/test/TableGen/generic-tables.td
@@ -25,33 +25,34 @@ include "llvm/TableGen/SearchableTable.td"
 // CHECK-LABEL: GET_ATable_IMPL
 // CHECK: constexpr AEntry ATable[] = {
 // CHECK-NOT:   { "aaa"
-// CHECK:       { "baz"
-// CHECK:       { "foo"
-// CHECK:       { "foobar"
-// CHECK:       { "bar"
+// CHECK:       { "baz", 0x2, 0x6, 0x0 },
+// CHECK:       { "foo", 0x4, 0x4, 0x0 },
+// CHECK:       { "foobar", 0x4, 0x5, 0x0 },
+// CHECK:       { "bar", 0x5, 0x3, 0x0 },
 // CHECK: };
 
 // CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
 // CHECK:   return &*Idx;
 // CHECK: }
 
-class AEntry<string str, int val1, int val2> {
+class AEntry<string str, int val1, int val2, bits<64> val3> {
   string Str = str;
   bits<8> Val1 = val1;
   bits<10> Val2 = val2;
+  bits<64> Val3 = val3;
   bit IsNeeded = 1;
 }
 
-def : AEntry<"aaa", 0, 0> { let IsNeeded = 0; }
-def : AEntry<"bar",    5, 3>;
-def : AEntry<"baz",    2, 6>;
-def : AEntry<"foo",    4, 4>;
-def : AEntry<"foobar", 4, 5>;
+def : AEntry<"aaa", 0, 0, 0> { let IsNeeded = 0; }
+def : AEntry<"bar",    5, 3, 0x100000000>;
+def : AEntry<"baz",    2, 6, 0xFFFFFFFF00000000>;
+def : AEntry<"foo",    4, 4, 0b0000000000000000000000000000000100000000000000000000000000000000>;
+def : AEntry<"foobar", 4, 5, 4294967296>;
 
 def ATable : GenericTable {
   let FilterClass = "AEntry";
   let FilterClassField = "IsNeeded";
-  let Fields = ["Str", "Val1", "Val2"];
+  let Fields = ["Str", "Val1", "Val2", "Val3"];
 
   let PrimaryKey = ["Val1", "Val2"];
   let PrimaryKeyName = "lookupATableByValues";

>From d91f1ee7437b28d98044fa11ce492c7c803a05ab Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Wed, 20 Sep 2023 15:06:12 +0800
Subject: [PATCH 2/2] [TableGen] Fix wrong bits output in GenericTable

We used to return `int` in `getAsInt`, while `IntInit::getValue`
returns `int64_t` and `utohexstr` needs `uint64_t`. The casting
causes the wrong hex value when printing bits value.

I split this PR into two commits to show the difference.
---
 llvm/test/TableGen/generic-tables.td           | 8 ++++----
 llvm/utils/TableGen/SearchableTableEmitter.cpp | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/test/TableGen/generic-tables.td b/llvm/test/TableGen/generic-tables.td
index 2528448f732c67b..003b53209346017 100644
--- a/llvm/test/TableGen/generic-tables.td
+++ b/llvm/test/TableGen/generic-tables.td
@@ -25,10 +25,10 @@ include "llvm/TableGen/SearchableTable.td"
 // CHECK-LABEL: GET_ATable_IMPL
 // CHECK: constexpr AEntry ATable[] = {
 // CHECK-NOT:   { "aaa"
-// CHECK:       { "baz", 0x2, 0x6, 0x0 },
-// CHECK:       { "foo", 0x4, 0x4, 0x0 },
-// CHECK:       { "foobar", 0x4, 0x5, 0x0 },
-// CHECK:       { "bar", 0x5, 0x3, 0x0 },
+// CHECK:       { "baz", 0x2, 0x6, 0xFFFFFFFF00000000 },
+// CHECK:       { "foo", 0x4, 0x4, 0x100000000 },
+// CHECK:       { "foobar", 0x4, 0x5, 0x100000000 },
+// CHECK:       { "bar", 0x5, 0x3, 0x100000000 },
 // CHECK: };
 
 // CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
diff --git a/llvm/utils/TableGen/SearchableTableEmitter.cpp b/llvm/utils/TableGen/SearchableTableEmitter.cpp
index ee1af0ec70df08b..9987d1ec73d9f4a 100644
--- a/llvm/utils/TableGen/SearchableTableEmitter.cpp
+++ b/llvm/utils/TableGen/SearchableTableEmitter.cpp
@@ -31,12 +31,12 @@ using namespace llvm;
 
 namespace {
 
-int getAsInt(Init *B) {
+int64_t getAsInt(Init *B) {
   return cast<IntInit>(
              B->convertInitializerTo(IntRecTy::get(B->getRecordKeeper())))
       ->getValue();
 }
-int getInt(Record *R, StringRef Field) {
+int64_t getInt(Record *R, StringRef Field) {
   return getAsInt(R->getValueInit(Field));
 }
 



More information about the llvm-commits mailing list