Skip to content

Commit d3c2785

Browse files
committed
WIP: tablegen: add hasTypeInfo field to DialectType
TODO: - align with the required upstream changes This allows users to hook into the (proposed) TargetExtTypeClass infrastructure to set the properties of dialect types. See: https://discourse.llvm.org/t/rfc-target-type-classes-for-extensibility-of-llvm-ir/69813 See: https://reviews.llvm.org/D147697
1 parent c72c998 commit d3c2785

File tree

12 files changed

+178
-32
lines changed

12 files changed

+178
-32
lines changed

example/ExampleDialect.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,15 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &out, VectorKind x) {
5252

5353
#define GET_DIALECT_DEFS
5454
#include "ExampleDialect.cpp.inc"
55+
56+
using namespace llvm;
57+
using namespace xd;
58+
59+
void XdVectorType::customizeTypeClass(TargetExtTypeClass *typeClass) {
60+
typeClass->setGetLayoutType([](TargetExtType *type) -> Type * {
61+
auto *vt = cast<XdVectorType>(type);
62+
if (vt->getKind() == VectorKind::MiddleEndian)
63+
return Type::getVoidTy(vt->getContext());
64+
return FixedVectorType::get(vt->getElementType(), vt->getNumElements());
65+
});
66+
}

example/ExampleDialect.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def XdVectorType : DialectType<ExampleDialect, "vector"> {
4444
let typeArguments = (args AttrVectorKind:$kind, type:$element_type,
4545
AttrI32:$num_elements);
4646

47+
let customizeTypeClass = true;
48+
4749
let summary = "a custom vector type";
4850
let description = [{
4951
Unlike LLVM's built-in vector type, this vector can have arbitrary element

example/ExampleMain.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ void createFunctionExample(Module &module, const Twine &name) {
8686
BasicBlock *bb = BasicBlock::Create(module.getContext(), "entry", fn);
8787
b.SetInsertPoint(bb);
8888

89+
Type *vt1 =
90+
xd::XdVectorType::get(xd::VectorKind::BigEndian, b.getInt32Ty(), 4);
91+
92+
Value *alloca1 = b.CreateAlloca(vt1);
93+
8994
Value *x1 = b.create<xd::ReadOp>(b.getInt32Ty());
9095
Value *sizeOf = b.create<xd::SizeOfOp>(b.getHalfTy());
9196
Value *sizeOf32 = b.create<xd::ITruncOp>(b.getInt32Ty(), sizeOf);
@@ -103,8 +108,10 @@ void createFunctionExample(Module &module, const Twine &name) {
103108

104109
Value *y1 = b.create<xd::ReadOp>(
105110
xd::XdVectorType::get(xd::VectorKind::BigEndian, b.getInt32Ty(), 4));
106-
Value *y2 = b.create<xd::ExtractElementOp>(y1, x1);
107-
Value *y3 = b.create<xd::ExtractElementOp>(y1, b.getInt32(2));
111+
b.CreateStore(y1, alloca1);
112+
Value *y1l = b.CreateLoad(vt1, alloca1);
113+
Value *y2 = b.create<xd::ExtractElementOp>(y1l, x1);
114+
Value *y3 = b.create<xd::ExtractElementOp>(y1l, b.getInt32(2));
108115
Value *y4 = b.CreateAdd(y2, y3);
109116
Value *y5 = b.create<xd::InsertElementOp>(q2, y4, x1);
110117
auto *y6 = b.create<xd::InsertElementOp>(y5, y2, b.getInt32(5));

include/llvm-dialects/Dialect/Dialect.td

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,19 @@ class DialectType<Dialect dialect_, string mnemonic_> : Type, Predicate {
194194
Dialect dialect = dialect_;
195195
string mnemonic = mnemonic_;
196196

197-
/// Whether the Type::get method has an explicit LLVMContext reference as the
197+
/// Whether the $Type::get method has an explicit LLVMContext reference as the
198198
/// first argument.
199199
bit defaultGetterHasExplicitContextArgument = false;
200200

201+
/// If set to true, a method of signature
202+
///
203+
/// static void customizeTypeClass(llvm::TargetExtTypeClass *typeClass);
204+
///
205+
/// is declared, to be defined manually by the user. The function is called
206+
/// only once and may adjust the type class. The type class is pre-populated
207+
/// with the type name and the default verifier.
208+
bit customizeTypeClass = false;
209+
201210
string summary = ?;
202211
string description = ?;
203212
}

include/llvm-dialects/TableGen/DialectType.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ class DialectType : public BaseCppPredicate {
4646
bool defaultGetterHasExplicitContextArgument() const {
4747
return m_defaultGetterHasExplicitContextArgument;
4848
}
49+
bool customizeTypeClass() const { return m_customizeTypeClass; }
4950
llvm::StringRef getSummary() const { return m_summary; }
5051
llvm::StringRef getDescription() const { return m_description; }
5152

53+
void emitTypeClass(llvm::raw_ostream &out, GenDialect *dialect,
54+
FmtContext &fmt) const;
5255
void emitDeclaration(llvm::raw_ostream &out, GenDialect *dialect) const;
5356
void emitDefinition(llvm::raw_ostream &out, GenDialect *dialect) const;
5457

@@ -62,6 +65,7 @@ class DialectType : public BaseCppPredicate {
6265
std::string m_name;
6366
std::string m_mnemonic;
6467
bool m_defaultGetterHasExplicitContextArgument = false;
68+
bool m_customizeTypeClass = false;
6569
std::string m_summary;
6670
std::string m_description;
6771

lib/TableGen/DialectType.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ bool DialectType::init(raw_ostream &errs, GenDialectsContext &context,
4949
m_defaultGetterHasExplicitContextArgument =
5050
record->getValueAsBit("defaultGetterHasExplicitContextArgument");
5151

52+
m_customizeTypeClass = record->getValueAsBit("customizeTypeClass");
53+
5254
FmtContext fmt;
5355
fmt.addSubst("_type", m_name);
5456
{
@@ -141,6 +143,35 @@ bool DialectType::init(raw_ostream &errs, GenDialectsContext &context,
141143
return true;
142144
}
143145

146+
void DialectType::emitTypeClass(llvm::raw_ostream &out, GenDialect *dialect,
147+
FmtContext &fmt) const {
148+
FmtContextScope scope(fmt);
149+
fmt.addSubst("dialect", dialect->name);
150+
fmt.addSubst("_type", getName());
151+
fmt.addSubst("mnemonic", getMnemonic());
152+
153+
if (m_customizeTypeClass) {
154+
fmt.addSubst("customize",
155+
tgfmt("$_type::customizeTypeClass(&theClass);", &fmt).str());
156+
} else {
157+
fmt.addSubst("customize", "");
158+
}
159+
160+
out << tgfmt(R"(
161+
static const auto class$_type = ([]() {
162+
::llvm::TargetExtTypeClass theClass("$dialect.$mnemonic");
163+
theClass.setVerifier(
164+
[](::llvm::TargetExtType *T, ::llvm::raw_ostream &errs) {
165+
return ::llvm::cast<$_type>(T)->verifier(errs);
166+
});
167+
$customize
168+
return theClass;
169+
})();
170+
$_context.registerTargetExtTypeClass(&class$_type);
171+
)",
172+
&fmt);
173+
}
174+
144175
void DialectType::emitDeclaration(raw_ostream &out, GenDialect *dialect) const {
145176
FmtContext fmt;
146177
fmt.withContext(m_context);
@@ -174,6 +205,12 @@ void DialectType::emitDeclaration(raw_ostream &out, GenDialect *dialect) const {
174205
}
175206
out << ");\n\n";
176207

208+
if (m_customizeTypeClass) {
209+
out << R"(
210+
static void customizeTypeClass(::llvm::TargetExtTypeClass *typeClass);
211+
)";
212+
}
213+
177214
for (const auto &argument : typeArguments()) {
178215
out << tgfmt("$0 get$1() const;\n", &fmt, argument.type->getCppType(),
179216
convertToCamelFromSnakeCase(argument.name, true));

lib/TableGen/GenDialect.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ void llvm_dialects::genDialectDecls(raw_ostream& out, RecordKeeper& records) {
7373
#include "llvm/IR/DerivedTypes.h"
7474
#include "llvm/IR/Instructions.h"
7575
76+
)";
77+
78+
if (!dialect->types.empty()) {
79+
out << R"(
80+
namespace llvm {
81+
class TargetExtTypeClass;
82+
} // namespace llvm
83+
)";
84+
}
85+
86+
out << R"(
7687
namespace llvm {
7788
class raw_ostream;
7889
} // namespace llvm
@@ -236,6 +247,12 @@ void llvm_dialects::genDialectDefs(raw_ostream& out, RecordKeeper& records) {
236247
)";
237248
}
238249

250+
if (!dialect->types.empty()) {
251+
out << R"(
252+
#include "llvm/IR/TargetExtType.h"
253+
)";
254+
}
255+
239256
out << R"(
240257
#include "llvm/Support/raw_ostream.h"
241258
#endif // GET_INCLUDES
@@ -322,6 +339,12 @@ void llvm_dialects::genDialectDefs(raw_ostream& out, RecordKeeper& records) {
322339
}
323340
}
324341

342+
FmtContextScope scope{fmt};
343+
fmt.withContext("context");
344+
345+
for (DialectType *type : dialect->types)
346+
type->emitTypeClass(out, dialect, fmt);
347+
325348
out << "}\n\n";
326349

327350
// Type class definitions.

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#
2424
#######################################################################################################################
2525

26-
set(LLVM_DIALECTS_TEST_DEPENDS FileCheck count not llvm-dialects-example)
26+
set(LLVM_DIALECTS_TEST_DEPENDS FileCheck count not split-file llvm-dialects-example)
2727
add_custom_target(llvm-dialects-test-depends DEPENDS ${LLVM_DIALECTS_TEST_DEPENDS})
2828
set_target_properties(llvm-dialects-test-depends PROPERTIES FOLDER "Tests")
2929

test/example/generated/ExampleDialect.cpp.inc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "llvm/Support/ModRef.h"
1414

15+
#include "llvm/IR/TargetExtType.h"
16+
1517
#include "llvm/Support/raw_ostream.h"
1618
#endif // GET_INCLUDES
1719

@@ -138,7 +140,29 @@ attrBuilder.addAttribute(::llvm::Attribute::NoUnwind);
138140
attrBuilder.addMemoryAttr(::llvm::MemoryEffects(::llvm::MemoryEffects::Location::InaccessibleMem, ::llvm::ModRefInfo::ModRef));
139141
m_attributeLists[3] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder);
140142
}
141-
}
143+
144+
static const auto classXdHandleType = ([]() {
145+
::llvm::TargetExtTypeClass theClass("xd.handle");
146+
theClass.setVerifier(
147+
[](::llvm::TargetExtType *T, ::llvm::raw_ostream &errs) {
148+
return ::llvm::cast<XdHandleType>(T)->verifier(errs);
149+
});
150+
151+
return theClass;
152+
})();
153+
context.registerTargetExtTypeClass(&classXdHandleType);
154+
155+
static const auto classXdVectorType = ([]() {
156+
::llvm::TargetExtTypeClass theClass("xd.vector");
157+
theClass.setVerifier(
158+
[](::llvm::TargetExtType *T, ::llvm::raw_ostream &errs) {
159+
return ::llvm::cast<XdVectorType>(T)->verifier(errs);
160+
});
161+
XdVectorType::customizeTypeClass(&theClass);
162+
return theClass;
163+
})();
164+
context.registerTargetExtTypeClass(&classXdVectorType);
165+
}
142166

143167
XdHandleType* XdHandleType::get(::llvm::LLVMContext & ctx) {
144168
::std::array<::llvm::Type *, 0> types = {

test/example/generated/ExampleDialect.h.inc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
#include "llvm/IR/DerivedTypes.h"
88
#include "llvm/IR/Instructions.h"
99

10+
11+
namespace llvm {
12+
class TargetExtTypeClass;
13+
} // namespace llvm
14+
1015
namespace llvm {
1116
class raw_ostream;
1217
} // namespace llvm
@@ -80,7 +85,9 @@ namespace xd {
8085

8186
static XdVectorType *get(VectorKind kind, ::llvm::Type * elementType, uint32_t numElements);
8287

83-
VectorKind getKind() const;
88+
89+
static void customizeTypeClass(::llvm::TargetExtTypeClass *typeClass);
90+
VectorKind getKind() const;
8491
::llvm::Type * getElementType() const;
8592
uint32_t getNumElements() const;
8693
};

0 commit comments

Comments
 (0)