Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions example/ExampleDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,42 @@ def BufferCompareOp : Op<ExampleDialect, "buffer.compare.op", [WillReturn, NoUnw
Both arguments get a parameter attribute, as well as return value
}];
}

def StructBackedType : DialectType<ExampleDialect, "struct.backed"> {
let summary = "a custom struct-backed type";
let description = [{
Test that a struct-backed type works correctly.
}];
let typeArguments = (args AttrI32:$field0, AttrI32:$field1, AttrI32:$field2);
let representation = (repr_struct (IntegerType 41));
let structPrefix = "sb_";

let defaultGetterHasExplicitContextArgument = 1;
}

def DummyStructBackedInpOp : Op<ExampleDialect, "struct.backed.inp.op", [WillReturn]> {
let summary = "a custom op using input arg with struct-backed type";
let description = [{
Test that an operation that takes argument with struct-backed type works correctly.
}];
let arguments = (ins
StructBackedType:$inp
);
let results = (outs
I32:$ret
);
}

def DummyStructBackedOutpOp : Op<ExampleDialect, "struct.backed.outp.op", [WillReturn]> {
let summary = "a custom op returning value with struct-backed type";
let description = [{
Test that an operation that returns value with struct-backed type works correctly.
}];
let arguments = (ins
I32:$extra_arg
);
let results = (outs
StructBackedType:$outp
);
let defaultBuilderHasExplicitResultType = true;
}
4 changes: 4 additions & 0 deletions example/ExampleMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ void createFunctionExample(Module &module, const Twine &name) {

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

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

b.CreateRetVoid();
}

Expand Down
11 changes: 11 additions & 0 deletions include/llvm-dialects/Dialect/Dialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ def F64 : SpecialBuiltinType<"Double">;
def F32 : SpecialBuiltinType<"Float">;
def F16 : SpecialBuiltinType<"Half">;

def repr_targetext;
def repr_struct;

/// All types that are defined by a dialect are derived from this class.
class DialectType<Dialect dialect_, string mnemonic_> : Type, Predicate {
dag typeArguments = ?;
Expand All @@ -229,6 +232,14 @@ class DialectType<Dialect dialect_, string mnemonic_> : Type, Predicate {

string summary = ?;
string description = ?;

/// How the dialect type is represented in LLVM IR:
/// - (repr_targetext): use a TargetExtType (the default)
/// - (repr_struct <type>): use a StructType with the given type as the
/// discriminant; the discriminant should be a type that cannot naturally
/// appear elsewhere, e.g. (repr_struct (IntegerType 41))
dag representation = (repr_targetext);
string structPrefix = "";
}

def and;
Expand Down
4 changes: 4 additions & 0 deletions include/llvm-dialects/TableGen/DialectType.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class DialectType : public BaseCppPredicate {
std::string m_context;
std::vector<GetterArg> m_getterArguments;
unsigned m_argBegin = 0;

bool m_structBacked = false;
unsigned m_structSentinelBitWidth;
std::string m_structPrefix;
};

} // namespace llvm_dialects
Loading