From 14580ce2fdd1898d130b20d9eb21bc4281868e7c Mon Sep 17 00:00:00 2001 From: "Paul C. Anagnostopoulos" Date: Mon, 5 Apr 2021 09:13:25 -0400 Subject: [PATCH] [TableGen] Make behavior of list slice suffix consistent across all values Differential Revision: https://reviews.llvm.org/D99883 --- llvm/lib/TableGen/Record.cpp | 3 + llvm/test/TableGen/ListSlices.td | 114 +++++++++++++++++++++++++++---- 2 files changed, 104 insertions(+), 13 deletions(-) diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index eeec9cafcc0d..2c8234da8431 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -615,6 +615,9 @@ Init *ListInit::convertInitializerTo(RecTy *Ty) const { } Init *ListInit::convertInitListSlice(ArrayRef Elements) const { + if (Elements.size() == 1) + return getElement(0); + SmallVector Vals; Vals.reserve(Elements.size()); for (unsigned Element : Elements) { diff --git a/llvm/test/TableGen/ListSlices.td b/llvm/test/TableGen/ListSlices.td index 2f40334798b2..82eeb28541ab 100644 --- a/llvm/test/TableGen/ListSlices.td +++ b/llvm/test/TableGen/ListSlices.td @@ -1,19 +1,107 @@ -// RUN: llvm-tblgen %s +// RUN: llvm-tblgen %s | FileCheck %s // XFAIL: vg_leak -def A { - list B = [10, 20, 30, 4, 1, 1231, 20]; +// This file has tests for the list slice suffix. + +// Test defvars and literal lists. + +// CHECK: def Rec00 +// CHECK: list ShowVar1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +// CHECK: int ShowVar2 = 0 +// CHECK: list ShowVar3 = [2, 3, 4, 5] +// CHECK: int ShowVar4 = 0 +// CHECK: list ShowVar5 = [2, 3, 4, 5] + +defvar Var1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; +defvar Var2 = Var1[0]; +defvar Var3 = Var1[2...5]; + +defvar Var4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][0]; +defvar Var5 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][2...5]; + +def Rec00 { // Display the defvars. + list ShowVar1 = Var1; + int ShowVar2 = Var2; + list ShowVar3 = Var3; + int ShowVar4 = Var4; + list ShowVar5 = Var5; } -def B { - list X = [10, 20, 30, 4, 1, 1231, 20] [2...4,2,2,0...6]; +// CHECK: def Rec01 +// CHECK: int Zero = 0 +// CHECK: list TwoFive = [2, 3, 4, 5] - list Y = X[4,5]; - int Z = X[4]; - - list C = A.B[1...4]; - - list> AA = [X, Y]; - - int BB = AA[0][1]; +def Rec01 { + int Zero = Var1[0]; + list TwoFive = Var1[2...5]; +} + +// Test class template arguments. + +// CHECK: def Rec02 +// CHECK: int Zero = 10 +// CHECK: list TwoFive = [12, 13, 14, 15] + +class Class1 ids> { + int Zero = ids[0]; + list TwoFive = ids[2...5]; +} + +def Rec02 : Class1<[10, 11, 12, 13, 14, 15, 16, 17]>; + +// Test anonymous record fetches. + +// CHECK: def Rec03 +// CHECK: int Zero = 20 +// CHECK: list TwoFive = [22, 23, 24, 25] + +def Rec03 { + int Zero = Class1<[20, 21, 22, 23, 24, 25, 26]>.Zero; + list TwoFive = Class1<[20, 21, 22, 23, 24, 25, 26]>.TwoFive; +} + +// Test multiclass template arguments. + +// CHECK: def Rec04_MC1 +// CHECK: int Zero = 30 +// CHECK: list TwoFive = [32, 33, 34, 35] +// CHECK: def Rec05_MC1 +// CHECK: int Zero = 30 +// CHECK: list TwoFive = [32, 33, 34, 35] + +multiclass MC1 ids> { + def _MC1 { + int Zero = ids[0]; + list TwoFive = ids[2...5]; + } +} + +defm Rec04 : MC1<[30, 31, 32, 33, 34, 35, 36]>; +defm Rec05 : MC1<[30, 31, 32, 33, 34, 35]>; + +// Test taking a complex subset of the list items from another record. + +// CHECK: def Rec07 +// CHECK: list SomeValues = [40, 43, 44, 45, 40, 47, 49, 48, 47, 46, 40] + +def Rec06 { + list Values = [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]; +} + +def Rec07 { + list SomeValues = Rec06.Values[0, 3...5, 0, 7, 9...6, 0]; +} + +// Test a double subscript. + +// CHECK: def Rec08 +// CHECK: list> Array = {{.*}}"foo", "bar", "snork"], ["zoo", "quux", "flarp"]] +// CHECK: string Snork = "snork" +// CHECK: list ZooQuux = ["zoo", "quux"] + +def Rec08 { + list> Array = [["foo", "bar", "snork"], + ["zoo", "quux", "flarp"]]; + string Snork = Array[0][2]; + list ZooQuux = Array[1][0...1]; }