Skip to content

Commit 3ff87c7

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.
1 parent a1a808c commit 3ff87c7

File tree

12 files changed

+142
-30
lines changed

12 files changed

+142
-30
lines changed

example/ExampleDialect.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ template <> struct llvm_dialects::Printable<xd::VectorKind> {
5151

5252
#define GET_DIALECT_DEFS
5353
#include "ExampleDialect.cpp.inc"
54+
55+
using namespace llvm;
56+
using namespace xd;
57+
58+
TargetTypeInfo XdVectorType::getTypeInfo() const {
59+
if (getKind() == VectorKind::MiddleEndian)
60+
return TargetTypeInfo(Type::getVoidTy(getContext()));
61+
62+
return TargetTypeInfo(
63+
FixedVectorType::get(getElementType(), getNumElements()));
64+
}

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 hasTypeInfo = 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,17 @@ class DialectType<Dialect dialect_, string mnemonic_> : Type, Predicate {
190190
Dialect dialect = dialect_;
191191
string mnemonic = mnemonic_;
192192

193-
/// Whether the Type::get method has an explicit LLVMContext reference as the
193+
/// Whether the $Type::get method has an explicit LLVMContext reference as the
194194
/// first argument.
195195
bit defaultGetterHasExplicitContextArgument = false;
196196

197+
/// If set to true, a method of signature
198+
///
199+
/// llvm::TargetTypeInfo getTypeInfo() const;
200+
///
201+
/// is declared, to be defined manually by the user.
202+
bit hasTypeInfo = false;
203+
197204
string summary = ?;
198205
string description = ?;
199206
}

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 hasTypeInfo() const { return m_hasTypeInfo; }
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_hasTypeInfo = false;
6569
std::string m_summary;
6670
std::string m_description;
6771

include/llvm-dialects/TableGen/Dialects.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class GenDialect {
5252
public:
5353
void finalize();
5454

55+
bool hasTypeInfo() const { return m_hasTypeInfo; }
56+
5557
llvm::ArrayRef<std::vector<Trait *>> attribute_lists() const {
5658
return m_attributeLists;
5759
}
@@ -60,6 +62,7 @@ class GenDialect {
6062

6163
private:
6264
std::vector<std::vector<Trait *>> m_attributeLists;
65+
bool m_hasTypeInfo = false;
6366
};
6467

6568
class GenDialectsContext {

lib/TableGen/DialectType.cpp

Lines changed: 26 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_hasTypeInfo = record->getValueAsBit("hasTypeInfo");
53+
5254
FmtContext fmt;
5355
fmt.addSubst("_type", m_name);
5456
{
@@ -141,6 +143,27 @@ 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+
if (!m_hasTypeInfo)
149+
return;
150+
151+
FmtContextScope scope(fmt);
152+
fmt.addSubst("dialect", dialect->name);
153+
fmt.addSubst("_type", getName());
154+
fmt.addSubst("mnemonic", getMnemonic());
155+
156+
out << tgfmt(R"(
157+
static const ::llvm::TargetExtTypeClass class$_type(
158+
"$dialect.$mnemonic", false,
159+
[](const ::llvm::TargetExtType *type) {
160+
return ::llvm::cast<$_type>(type)->getTypeInfo();
161+
});
162+
$_context.registerTargetExtTypeClass(&class$_type);
163+
)",
164+
&fmt);
165+
}
166+
144167
void DialectType::emitDeclaration(raw_ostream &out, GenDialect *dialect) const {
145168
FmtContext fmt;
146169
fmt.withContext(m_context);
@@ -174,6 +197,9 @@ void DialectType::emitDeclaration(raw_ostream &out, GenDialect *dialect) const {
174197
}
175198
out << ");\n\n";
176199

200+
if (m_hasTypeInfo)
201+
out << "::llvm::TargetTypeInfo getTypeInfo() const;\n\n";
202+
177203
for (const auto &argument : typeArguments()) {
178204
out << tgfmt("$0 get$1() const;\n", &fmt, argument.type->getCppType(),
179205
convertToCamelFromSnakeCase(argument.name, true));

lib/TableGen/Dialects.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ void GenDialect::finalize() {
6060
}
6161
op->m_attributeListIdx = m_attributeLists.size() - 1;
6262
}
63+
64+
// Check if we have a type that has type info
65+
for (DialectType *type : types) {
66+
if (type->hasTypeInfo()) {
67+
m_hasTypeInfo = true;
68+
break;
69+
}
70+
}
6371
}
6472

6573
GenDialectsContext::GenDialectsContext() = default;

lib/TableGen/GenDialect.cpp

Lines changed: 25 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->hasTypeInfo()) {
79+
out << R"(
80+
namespace llvm {
81+
class TargetTypeInfo;
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->hasTypeInfo()) {
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,14 @@ void llvm_dialects::genDialectDefs(raw_ostream& out, RecordKeeper& records) {
322339
}
323340
}
324341

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

327352
// Type class definitions.

test/example/generated/ExampleDialect.cpp.inc

Lines changed: 10 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,14 @@ 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 ::llvm::TargetExtTypeClass classXdVectorType(
145+
"xd.vector", false,
146+
[](const ::llvm::TargetExtType *type) {
147+
return ::llvm::cast<XdVectorType>(type)->getTypeInfo();
148+
});
149+
context.registerTargetExtTypeClass(&classXdVectorType);
150+
}
142151

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

0 commit comments

Comments
 (0)