Skip to content

Commit a9dc867

Browse files
piotrAMDnhaehnle
andcommitted
Support struct-backed type
Support struct-backed type, which is a custom struct type where: 1. the first field is a sentinel of integer type 2. each integer argument N passed during constructing the type is treated as an unsigned int encoded as a struct field: - intN (integer with N bit width) for positive N (up to MAX_INT_BITS) - empty structure for N == 0 For example StructBackedType::get(ctx, 1, 0, 2) gets encoded as %sb_1.0.2. = type { i41, i1, {}, i2 } Co-authored-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
1 parent 2d56785 commit a9dc867

8 files changed

Lines changed: 620 additions & 85 deletions

File tree

example/ExampleDialect.td

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,42 @@ def BufferCompareOp : Op<ExampleDialect, "buffer.compare.op", [WillReturn, NoUnw
361361
Both arguments get a parameter attribute, as well as return value
362362
}];
363363
}
364+
365+
def StructBackedType : DialectType<ExampleDialect, "struct.backed"> {
366+
let summary = "a custom struct-backed type";
367+
let description = [{
368+
Test that a struct-backed type works correctly.
369+
}];
370+
let typeArguments = (args AttrI32:$field0, AttrI32:$field1, AttrI32:$field2);
371+
let representation = (repr_struct (IntegerType 41));
372+
let structPrefix = "sb_";
373+
374+
let defaultGetterHasExplicitContextArgument = 1;
375+
}
376+
377+
def DummyStructBackedInpOp : Op<ExampleDialect, "struct.backed.inp.op", [WillReturn]> {
378+
let summary = "a custom op using input arg with struct-backed type";
379+
let description = [{
380+
Test that an operation that takes argument with struct-backed type works correctly.
381+
}];
382+
let arguments = (ins
383+
StructBackedType:$inp
384+
);
385+
let results = (outs
386+
I32:$ret
387+
);
388+
}
389+
390+
def DummyStructBackedOutpOp : Op<ExampleDialect, "struct.backed.outp.op", [WillReturn]> {
391+
let summary = "a custom op returning value with struct-backed type";
392+
let description = [{
393+
Test that an operation that returns value with struct-backed type works correctly.
394+
}];
395+
let arguments = (ins
396+
I32:$extra_arg
397+
);
398+
let results = (outs
399+
StructBackedType:$outp
400+
);
401+
let defaultBuilderHasExplicitResultType = true;
402+
}

example/ExampleMain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ 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);
153+
auto *structBackedVal = b.create<xd::cpp::DummyStructBackedOutpOp>(structBackedTy, b.getInt32(42), "gen.struct.backed.val");
154+
b.create<xd::cpp::DummyStructBackedInpOp>(structBackedVal, "consume.struct.backed.val");
155+
152156
b.CreateRetVoid();
153157
}
154158

include/llvm-dialects/Dialect/Dialect.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ def F64 : SpecialBuiltinType<"Double">;
215215
def F32 : SpecialBuiltinType<"Float">;
216216
def F16 : SpecialBuiltinType<"Half">;
217217

218+
def repr_targetext;
219+
def repr_struct;
220+
218221
/// All types that are defined by a dialect are derived from this class.
219222
class DialectType<Dialect dialect_, string mnemonic_> : Type, Predicate {
220223
dag typeArguments = ?;
@@ -229,6 +232,14 @@ class DialectType<Dialect dialect_, string mnemonic_> : Type, Predicate {
229232

230233
string summary = ?;
231234
string description = ?;
235+
236+
/// How the dialect type is represented in LLVM IR:
237+
/// - (repr_targetext): use a TargetExtType (the default)
238+
/// - (repr_struct <type>): use a StructType with the given type as the
239+
/// discriminant; the discriminant should be a type that cannot naturally
240+
/// appear elsewhere, e.g. (repr_struct (IntegerType 41))
241+
dag representation = (repr_targetext);
242+
string structPrefix = "";
232243
}
233244

234245
def and;

include/llvm-dialects/TableGen/DialectType.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class DialectType : public BaseCppPredicate {
7575
std::string m_context;
7676
std::vector<GetterArg> m_getterArguments;
7777
unsigned m_argBegin = 0;
78+
79+
bool m_structBacked = false;
80+
unsigned m_structSentinelBitWidth;
81+
std::string m_structPrefix;
7882
};
7983

8084
} // namespace llvm_dialects

0 commit comments

Comments
 (0)