[llvm] a022be6 - [TableGen] Enhance !cast<string> to handle bit and bits types.

Paul C. Anagnostopoulos via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 14 07:21:01 PST 2021


Author: Paul C. Anagnostopoulos
Date: 2021-01-14T10:20:35-05:00
New Revision: a022be625387370cf67b26c6b99b05f16b2a8610

URL: https://github.com/llvm/llvm-project/commit/a022be625387370cf67b26c6b99b05f16b2a8610
DIFF: https://github.com/llvm/llvm-project/commit/a022be625387370cf67b26c6b99b05f16b2a8610.diff

LOG: [TableGen] Enhance !cast<string> to handle bit and bits types.

Add a test for this.

Differential Revision: https://reviews.llvm.org/D94529

Added: 
    llvm/test/TableGen/cast-string.td

Modified: 
    llvm/lib/TableGen/Record.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index d047b7bdf1cd..74786e040018 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -689,8 +689,10 @@ Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
       if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
         return StringInit::get(LHSd->getAsString());
 
-      if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
+      if (IntInit *LHSi =
+              dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())))
         return StringInit::get(LHSi->getAsString());
+
     } else if (isa<RecordRecTy>(getType())) {
       if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
         if (!CurRec && !IsFinal)

diff  --git a/llvm/test/TableGen/cast-string.td b/llvm/test/TableGen/cast-string.td
new file mode 100644
index 000000000000..be25d24a3b9f
--- /dev/null
+++ b/llvm/test/TableGen/cast-string.td
@@ -0,0 +1,59 @@
+// RUN: llvm-tblgen %s | FileCheck %s
+// RUN: not llvm-tblgen -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
+
+// This file tests the !cast bang operator with the result type string.
+
+defvar IntList = [0, 1, 2, 3, 4, 5, 6];
+
+// CHECK: def Rec0
+// CHECK:   string str3 = "a string here"
+
+def Rec0 {
+  string str = "a string";
+  string str2 = !cast<string>(str);
+  string str3 = !cast<string>(str # " here");
+}
+
+// CHECK: def Rec1
+// CHECK:   string str = "42 -108"
+
+def Rec1 {
+  int int1 = 42;
+  int int2 = -108;
+  string str = !cast<string>(int1) # " " # !cast<string>(int2);
+}
+
+// CHECK: def Rec2
+// CHECK:   string str = "0, 1"
+
+def Rec2 {
+  bit bit1 = false;
+  bit bit2 = true;
+  string str = !cast<string>(bit1) # ", " # !cast<string>(bit2);
+}
+
+// CHECK: def Rec3
+// CHECK:   string str = "5 and 37"
+
+def Rec3 {
+  bits<4> bits1 = 0b0101;
+  bits<8> bits2 = 0b00100101;
+  string str = !cast<string>(bits1) # " and " # !cast<string>(bits2);
+}
+
+// CHECK: def Rec4
+// CHECK:   string str = "Rec1, Rec2"
+
+def Rec4 {
+  string str = !cast<string>(Rec1) # ", " # !cast<string>(Rec2);
+}
+
+#ifdef ERROR1
+
+// ERROR1: nitializer of 'str' in 'Rec5' could not be fully resolved
+
+def Rec5 {
+  string str = !cast<string>(IntList);
+}
+
+#endif


        


More information about the llvm-commits mailing list