Skip to content

Commit 418a80d

Browse files
committed
Support enum class in struct-backed type
1 parent 8620e69 commit 418a80d

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

example/ExampleDialect.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def StructBackedType : DialectType<ExampleDialect, "struct.backed"> {
367367
let description = [{
368368
Test that a struct-backed type works correctly.
369369
}];
370-
let typeArguments = (args AttrI32:$field0, AttrI32:$field1, AttrI32:$field2);
370+
let typeArguments = (args AttrI32:$field0, AttrI8:$field1, AttrVectorKind:$field2);
371371
let representation = (repr_struct (IntegerType 41));
372372

373373
let defaultGetterHasExplicitContextArgument = 1;

example/ExampleMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void createFunctionExample(Module &module, const Twine &name) {
149149

150150
b.create<xd::cpp::StringAttrOp>("Hello world!");
151151

152-
xd::cpp::StructBackedType *structBackedTy = xd::cpp::StructBackedType::get(bb->getContext(), 1, 0, 2);
152+
xd::cpp::StructBackedType *structBackedTy = xd::cpp::StructBackedType::get(bb->getContext(), 1, 0, xd::cpp::VectorKind::BigEndian);
153153
auto *structBackedVal = b.create<xd::cpp::DummyStructBackedOutpOp>(structBackedTy, b.getInt32(42), "gen.struct.backed.val");
154154
b.create<xd::cpp::DummyStructBackedInpOp>(structBackedVal, "consume.struct.backed.val");
155155

lib/TableGen/DialectType.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,14 @@ void DialectType::emitDeclaration(raw_ostream &out, GenDialect *dialect) const {
204204
for (const auto &argument : typeArguments()) {
205205
std::string camel = convertToCamelFromSnakeCase(argument.name, true);
206206
out << tgfmt(
207-
R"( unsigned get$0() const {
208-
::llvm::Type *elt = getElementType($1);
207+
R"( $0 get$1() const {
208+
::llvm::Type *elt = getElementType($2);
209209
if (elt->isStructTy())
210-
return 0;
211-
return ::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
210+
return ($0)0;
211+
return ($0)::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
212212
}
213213
)",
214-
&fmt, camel, fieldIdx++);
214+
&fmt, argument.type->getCppType(), camel, fieldIdx++);
215215
}
216216

217217
out << " };\n\n";
@@ -309,10 +309,10 @@ void DialectType::emitDefinition(raw_ostream &out, GenDialect *dialect) const {
309309

310310
for (const auto &getterArg : getterArgs) {
311311
out << tgfmt(R"(
312-
if ($0 == 0)
312+
if ((uint64_t)$0 == 0)
313313
$fields.push_back(::llvm::StructType::get($_context));
314314
else
315-
$fields.push_back(::llvm::IntegerType::get($_context, $0));
315+
$fields.push_back(::llvm::IntegerType::get($_context, (uint64_t)$0));
316316
)",
317317
&fmt, getterArg.name);
318318
}

test/example/generated/ExampleDialect.cpp.inc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ m_attributeLists[6] = argAttrList.addFnAttributes(context, attrBuilder);
258258
}
259259
}
260260

261-
StructBackedType* StructBackedType::get(::llvm::LLVMContext & ctx, uint32_t field0, uint32_t field1, uint32_t field2) {
262-
261+
StructBackedType* StructBackedType::get(::llvm::LLVMContext & ctx, uint32_t field0, uint8_t field1, VectorKind field2) {
263262

264263

264+
static_assert(sizeof(field2) <= sizeof(unsigned));
265265
std::string name; ::llvm::raw_string_ostream os(name);
266266
os << "struct.backed";
267267
os << '.' << (uint64_t)field0;
@@ -270,20 +270,20 @@ StructBackedType* StructBackedType::get(::llvm::LLVMContext & ctx, uint32_t fiel
270270
::std::vector<::llvm::Type*> fields;
271271
fields.push_back(::llvm::IntegerType::get(ctx, 41));
272272

273-
if (field0 == 0)
273+
if ((uint64_t)field0 == 0)
274274
fields.push_back(::llvm::StructType::get(ctx));
275275
else
276-
fields.push_back(::llvm::IntegerType::get(ctx, field0));
276+
fields.push_back(::llvm::IntegerType::get(ctx, (uint64_t)field0));
277277

278-
if (field1 == 0)
278+
if ((uint64_t)field1 == 0)
279279
fields.push_back(::llvm::StructType::get(ctx));
280280
else
281-
fields.push_back(::llvm::IntegerType::get(ctx, field1));
281+
fields.push_back(::llvm::IntegerType::get(ctx, (uint64_t)field1));
282282

283-
if (field2 == 0)
283+
if ((uint64_t)field2 == 0)
284284
fields.push_back(::llvm::StructType::get(ctx));
285285
else
286-
fields.push_back(::llvm::IntegerType::get(ctx, field2));
286+
fields.push_back(::llvm::IntegerType::get(ctx, (uint64_t)field2));
287287
auto *st = ::llvm::StructType::create(ctx, fields, os.str(), /*isPacked=*/false);
288288
return static_cast<StructBackedType *>(st);
289289
}

test/example/generated/ExampleDialect.h.inc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,27 @@ namespace xd::cpp {
5858
using ::llvm::StructType::getElementType;
5959

6060
static StructBackedType *get(
61-
::llvm::LLVMContext & ctx, uint32_t field0, uint32_t field1, uint32_t field2);
61+
::llvm::LLVMContext & ctx, uint32_t field0, uint8_t field1, VectorKind field2);
6262

6363
static bool classof(const ::llvm::Type *t);
6464

65-
unsigned getField0() const {
65+
uint32_t getField0() const {
6666
::llvm::Type *elt = getElementType(1);
6767
if (elt->isStructTy())
68-
return 0;
69-
return ::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
68+
return (uint32_t)0;
69+
return (uint32_t)::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
7070
}
71-
unsigned getField1() const {
71+
uint8_t getField1() const {
7272
::llvm::Type *elt = getElementType(2);
7373
if (elt->isStructTy())
74-
return 0;
75-
return ::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
74+
return (uint8_t)0;
75+
return (uint8_t)::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
7676
}
77-
unsigned getField2() const {
77+
VectorKind getField2() const {
7878
::llvm::Type *elt = getElementType(3);
7979
if (elt->isStructTy())
80-
return 0;
81-
return ::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
80+
return (VectorKind)0;
81+
return (VectorKind)::llvm::cast<::llvm::IntegerType>(elt)->getBitWidth();
8282
}
8383
};
8484

0 commit comments

Comments
 (0)