Skip to content

Commit 8620e69

Browse files
piotrAMDnhaehnle
andauthored
Support struct-backed type (#132)
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 <[email protected]>
1 parent 2d56785 commit 8620e69

File tree

8 files changed

+617
-85
lines changed

8 files changed

+617
-85
lines changed

example/ExampleDialect.td

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,41 @@ 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+
373+
let defaultGetterHasExplicitContextArgument = 1;
374+
}
375+
376+
def DummyStructBackedInpOp : Op<ExampleDialect, "struct.backed.inp.op", [WillReturn]> {
377+
let summary = "a custom op using input arg with struct-backed type";
378+
let description = [{
379+
Test that an operation that takes argument with struct-backed type works correctly.
380+
}];
381+
let arguments = (ins
382+
StructBackedType:$inp
383+
);
384+
let results = (outs
385+
I32:$ret
386+
);
387+
}
388+
389+
def DummyStructBackedOutpOp : Op<ExampleDialect, "struct.backed.outp.op", [WillReturn]> {
390+
let summary = "a custom op returning value with struct-backed type";
391+
let description = [{
392+
Test that an operation that returns value with struct-backed type works correctly.
393+
}];
394+
let arguments = (ins
395+
I32:$extra_arg
396+
);
397+
let results = (outs
398+
StructBackedType:$outp
399+
);
400+
let defaultBuilderHasExplicitResultType = true;
401+
}

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: 10 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,13 @@ 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);
232242
}
233243

234244
def and;

include/llvm-dialects/TableGen/DialectType.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ 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;
7881
};
7982

8083
} // namespace llvm_dialects

0 commit comments

Comments
 (0)