Skip to content

Commit 0b96909

Browse files
tsymallaThomas Symalla
andauthored
Fix namespace issue in isDialectOp (#106)
Use $dialect instead of cppNamespace when generating IsDialectOp helper --------- Co-authored-by: Thomas Symalla <[email protected]>
1 parent bdfb113 commit 0b96909

File tree

12 files changed

+260
-258
lines changed

12 files changed

+260
-258
lines changed

example/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ set(EXAMPLE_TABLEGEN_EXE llvm-dialects-tblgen)
2323
set(EXAMPLE_TABLEGEN_TARGET llvm-dialects-tblgen)
2424
set(LLVM_TARGET_DEFINITIONS ExampleDialect.td)
2525

26-
tablegen(EXAMPLE ExampleDialect.h.inc -gen-dialect-decls --dialect xd
26+
tablegen(EXAMPLE ExampleDialect.h.inc -gen-dialect-decls --dialect xd.ir
2727
EXTRA_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../include)
28-
tablegen(EXAMPLE ExampleDialect.cpp.inc -gen-dialect-defs --dialect xd
28+
tablegen(EXAMPLE ExampleDialect.cpp.inc -gen-dialect-defs --dialect xd.ir
2929
EXTRA_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../include)
3030
add_public_tablegen_target(ExampleDialectTableGen)
3131

example/ExampleDialect.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define GET_INCLUDES
2929
#include "ExampleDialect.cpp.inc"
3030

31-
namespace xd {
31+
namespace xd::cpp {
3232

3333
const char *toString(VectorKind kind) {
3434
switch (kind) {
@@ -48,7 +48,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &out, VectorKind x) {
4848
return out;
4949
}
5050

51-
} // namespace xd
51+
} // namespace xd::cpp
5252

5353
#define GET_DIALECT_DEFS
5454
#include "ExampleDialect.cpp.inc"

example/ExampleDialect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define GET_INCLUDES
2929
#include "ExampleDialect.h.inc"
3030

31-
namespace xd {
31+
namespace xd::cpp {
3232

3333
enum class VectorKind {
3434
LittleEndian = 0,
@@ -38,7 +38,7 @@ enum class VectorKind {
3838

3939
const char *toString(VectorKind kind);
4040

41-
} // namespace xd
41+
} // namespace xd::cpp
4242

4343
#define GET_DIALECT_DECLS
4444
#include "ExampleDialect.h.inc"

example/ExampleDialect.td

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
include "llvm-dialects/Dialect/Dialect.td"
2727

2828
def ExampleDialect : Dialect {
29-
let name = "xd";
30-
let cppNamespace = "xd";
29+
let name = "xd.ir";
30+
let cppNamespace = "xd::cpp";
3131
}
3232

3333
defm AttrVectorKind : AttrEnum<"VectorKind">;
3434

35-
def VectorKindLittleEndian : CppConstant<"xd::VectorKind::LittleEndian">;
36-
def VectorKindBigEndian : CppConstant<"xd::VectorKind::BigEndian">;
37-
def VectorKindMiddleEndian : CppConstant<"xd::VectorKind::MiddleEndian">;
35+
def VectorKindLittleEndian : CppConstant<"xd::cpp::VectorKind::LittleEndian">;
36+
def VectorKindBigEndian : CppConstant<"xd::cpp::VectorKind::BigEndian">;
37+
def VectorKindMiddleEndian : CppConstant<"xd::cpp::VectorKind::MiddleEndian">;
3838

3939
def ImmutableAttrI1 : IntegerAttr<"bool"> {
4040
let isImmutable = true;

example/ExampleMain.cpp

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ void useUnnamedStructTypes(Builder &b) {
6969
StructType *t2 = StructType::create({b.getInt32Ty(), b.getInt32Ty()}, "");
7070
StructType *t3 = StructType::create({b.getInt64Ty()}, "");
7171

72-
Value *z1 = b.create<xd::ReadOp>(t1);
73-
Value *z2 = b.create<xd::ReadOp>(t2);
74-
Value *z3 = b.create<xd::ReadOp>(t3);
75-
b.create<xd::WriteOp>(z1);
76-
b.create<xd::WriteOp>(z2);
77-
b.create<xd::WriteOp>(z3);
72+
Value *z1 = b.create<xd::cpp::ReadOp>(t1);
73+
Value *z2 = b.create<xd::cpp::ReadOp>(t2);
74+
Value *z3 = b.create<xd::cpp::ReadOp>(t3);
75+
b.create<xd::cpp::WriteOp>(z1);
76+
b.create<xd::cpp::WriteOp>(z2);
77+
b.create<xd::cpp::WriteOp>(z3);
7878
}
7979

8080
void createFunctionExample(Module &module, const Twine &name) {
@@ -86,47 +86,47 @@ void createFunctionExample(Module &module, const Twine &name) {
8686
BasicBlock *bb = BasicBlock::Create(module.getContext(), "entry", fn);
8787
b.SetInsertPoint(bb);
8888

89-
Value *x1 = b.create<xd::ReadOp>(b.getInt32Ty());
90-
Value *sizeOf = b.create<xd::SizeOfOp>(b.getHalfTy());
91-
Value *sizeOf32 = b.create<xd::ITruncOp>(b.getInt32Ty(), sizeOf);
92-
Value *x2 = b.create<xd::Add32Op>(x1, sizeOf32, 11);
93-
Value *x3 = b.create<xd::CombineOp>(x2, x1);
94-
Value *x4 = b.create<xd::IExtOp>(b.getInt64Ty(), x3);
95-
b.create<xd::WriteOp>(x4);
96-
97-
cast<xd::SizeOfOp>(sizeOf)->setSizeofType(b.getDoubleTy());
98-
cast<xd::Add32Op>(x2)->setExtra(7);
99-
100-
Value *q1 = b.create<xd::ReadOp>(FixedVectorType::get(b.getInt32Ty(), 2));
101-
Value *q2 = b.create<xd::FromFixedVectorOp>(
102-
xd::XdVectorType::get(xd::VectorKind::BigEndian, b.getInt32Ty(), 2), q1);
103-
104-
Value *y1 = b.create<xd::ReadOp>(
105-
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));
89+
Value *x1 = b.create<xd::cpp::ReadOp>(b.getInt32Ty());
90+
Value *sizeOf = b.create<xd::cpp::SizeOfOp>(b.getHalfTy());
91+
Value *sizeOf32 = b.create<xd::cpp::ITruncOp>(b.getInt32Ty(), sizeOf);
92+
Value *x2 = b.create<xd::cpp::Add32Op>(x1, sizeOf32, 11);
93+
Value *x3 = b.create<xd::cpp::CombineOp>(x2, x1);
94+
Value *x4 = b.create<xd::cpp::IExtOp>(b.getInt64Ty(), x3);
95+
b.create<xd::cpp::WriteOp>(x4);
96+
97+
cast<xd::cpp::SizeOfOp>(sizeOf)->setSizeofType(b.getDoubleTy());
98+
cast<xd::cpp::Add32Op>(x2)->setExtra(7);
99+
100+
Value *q1 = b.create<xd::cpp::ReadOp>(FixedVectorType::get(b.getInt32Ty(), 2));
101+
Value *q2 = b.create<xd::cpp::FromFixedVectorOp>(
102+
xd::cpp::XdVectorType::get(xd::cpp::VectorKind::BigEndian, b.getInt32Ty(), 2), q1);
103+
104+
Value *y1 = b.create<xd::cpp::ReadOp>(
105+
xd::cpp::XdVectorType::get(xd::cpp::VectorKind::BigEndian, b.getInt32Ty(), 4));
106+
Value *y2 = b.create<xd::cpp::ExtractElementOp>(y1, x1);
107+
Value *y3 = b.create<xd::cpp::ExtractElementOp>(y1, b.getInt32(2));
108108
Value *y4 = b.CreateAdd(y2, y3);
109-
Value *y5 = b.create<xd::InsertElementOp>(q2, y4, x1);
110-
auto *y6 = b.create<xd::InsertElementOp>(y5, y2, b.getInt32(5));
111-
b.create<xd::WriteOp>(y6);
109+
Value *y5 = b.create<xd::cpp::InsertElementOp>(q2, y4, x1);
110+
auto *y6 = b.create<xd::cpp::InsertElementOp>(y5, y2, b.getInt32(5));
111+
b.create<xd::cpp::WriteOp>(y6);
112112

113113
y6->setIndex(b.getInt32(1));
114114

115-
Value *p1 = b.create<xd::ReadOp>(b.getPtrTy(0));
115+
Value *p1 = b.create<xd::cpp::ReadOp>(b.getPtrTy(0));
116116
p1->setName("p1");
117-
Value *p2 = b.create<xd::StreamAddOp>(p1, b.getInt64(14), b.getInt8(0));
117+
Value *p2 = b.create<xd::cpp::StreamAddOp>(p1, b.getInt64(14), b.getInt8(0));
118118
p2->setName("p2");
119-
b.create<xd::WriteOp>(p2);
119+
b.create<xd::cpp::WriteOp>(p2);
120120

121-
assert(xd::ExampleDialect::isDialectOp(*cast<CallInst>(p2)));
121+
assert(xd::cpp::ExampleDialect::isDialectOp(*cast<CallInst>(p2)));
122122

123123
SmallVector<Value *> varArgs;
124124
varArgs.push_back(p1);
125125
varArgs.push_back(p2);
126-
b.create<xd::WriteVarArgOp>(p2, varArgs);
127-
b.create<xd::HandleGetOp>();
126+
b.create<xd::cpp::WriteVarArgOp>(p2, varArgs);
127+
b.create<xd::cpp::HandleGetOp>();
128128

129-
auto *replaceable = b.create<xd::WriteVarArgOp>(p2, varArgs);
129+
auto *replaceable = b.create<xd::cpp::WriteVarArgOp>(p2, varArgs);
130130
SmallVector<Metadata *, 1> MD;
131131
MD.push_back(ConstantAsMetadata::get(
132132
ConstantInt::get(Type::getInt32Ty(bb->getContext()), 1)));
@@ -136,25 +136,25 @@ void createFunctionExample(Module &module, const Twine &name) {
136136
varArgs2.push_back(p2);
137137

138138
replaceable->replaceArgs(varArgs2);
139-
b.create<xd::SetReadOp>(FixedVectorType::get(b.getInt32Ty(), 2));
140-
b.create<xd::SetWriteOp>(y6);
139+
b.create<xd::cpp::SetReadOp>(FixedVectorType::get(b.getInt32Ty(), 2));
140+
b.create<xd::cpp::SetWriteOp>(y6);
141141

142142
useUnnamedStructTypes(b);
143143

144-
b.create<xd::HandleGetOp>("name.of.llvm.value");
145-
b.create<xd::InstNameConflictOp>(b.getInt32(1));
146-
b.create<xd::InstNameConflictOp>(b.getInt32(1), "name.foo");
147-
b.create<xd::InstNameConflictDoubleOp>(b.getInt32(1), b.getInt32(2));
148-
b.create<xd::InstNameConflictDoubleOp>(b.getInt32(1), b.getInt32(2), "bar");
144+
b.create<xd::cpp::HandleGetOp>("name.of.llvm.value");
145+
b.create<xd::cpp::InstNameConflictOp>(b.getInt32(1));
146+
b.create<xd::cpp::InstNameConflictOp>(b.getInt32(1), "name.foo");
147+
b.create<xd::cpp::InstNameConflictDoubleOp>(b.getInt32(1), b.getInt32(2));
148+
b.create<xd::cpp::InstNameConflictDoubleOp>(b.getInt32(1), b.getInt32(2), "bar");
149149
SmallVector<Value *> moreVarArgs = varArgs;
150-
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs);
151-
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs, "two.varargs");
150+
b.create<xd::cpp::InstNameConflictVarargsOp>(moreVarArgs);
151+
b.create<xd::cpp::InstNameConflictVarargsOp>(moreVarArgs, "two.varargs");
152152
moreVarArgs.push_back(b.getInt32(3));
153-
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs, "three.varargs");
153+
b.create<xd::cpp::InstNameConflictVarargsOp>(moreVarArgs, "three.varargs");
154154
moreVarArgs.push_back(b.getInt32(4));
155-
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs, "four.varargs");
155+
b.create<xd::cpp::InstNameConflictVarargsOp>(moreVarArgs, "four.varargs");
156156

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

159159
b.CreateRetVoid();
160160
}
@@ -210,20 +210,20 @@ template <bool rpot> const Visitor<VisitorContainer> &getExampleVisitor() {
210210
static const auto visitor =
211211
VisitorBuilder<VisitorContainer>()
212212
.nest<VisitorNest>([](VisitorBuilder<VisitorNest> &b) {
213-
b.add<xd::ReadOp>([](VisitorNest &self, xd::ReadOp &op) {
213+
b.add<xd::cpp::ReadOp>([](VisitorNest &self, xd::cpp::ReadOp &op) {
214214
*self.out << "visiting ReadOp: " << op << '\n';
215215
});
216216
b.add(&VisitorNest::visitUnaryInstruction);
217-
b.add<xd::SetReadOp>([](VisitorNest &self, xd::SetReadOp &op) {
217+
b.add<xd::cpp::SetReadOp>([](VisitorNest &self, xd::cpp::SetReadOp &op) {
218218
*self.out << "visiting SetReadOp: " << op << '\n';
219219
return op.getType()->isIntegerTy(1) ? VisitorResult::Stop
220220
: VisitorResult::Continue;
221221
});
222-
b.addSet<xd::SetReadOp, xd::SetWriteOp>(
222+
b.addSet<xd::cpp::SetReadOp, xd::cpp::SetWriteOp>(
223223
[](VisitorNest &self, llvm::Instruction &op) {
224-
if (isa<xd::SetReadOp>(op)) {
224+
if (isa<xd::cpp::SetReadOp>(op)) {
225225
*self.out << "visiting SetReadOp (set): " << op << '\n';
226-
} else if (isa<xd::SetWriteOp>(op)) {
226+
} else if (isa<xd::cpp::SetWriteOp>(op)) {
227227
*self.out << "visiting SetWriteOp (set): " << op << '\n';
228228
}
229229
});
@@ -248,17 +248,17 @@ template <bool rpot> const Visitor<VisitorContainer> &getExampleVisitor() {
248248
});
249249
b.add(&VisitorNest::visitBinaryOperator);
250250
b.nest<raw_ostream>([](VisitorBuilder<raw_ostream> &b) {
251-
b.add<xd::WriteOp>([](raw_ostream &out, xd::WriteOp &op) {
251+
b.add<xd::cpp::WriteOp>([](raw_ostream &out, xd::cpp::WriteOp &op) {
252252
out << "visiting WriteOp: " << op << '\n';
253253
});
254-
b.add<xd::WriteVarArgOp>(
255-
[](raw_ostream &out, xd::WriteVarArgOp &op) {
254+
b.add<xd::cpp::WriteVarArgOp>(
255+
[](raw_ostream &out, xd::cpp::WriteVarArgOp &op) {
256256
out << "visiting WriteVarArgOp: " << op << ":\n";
257257
for (Value *arg : op.getArgs())
258258
out << " " << *arg << '\n';
259259
});
260-
b.add<xd::StringAttrOp>(
261-
[](raw_ostream &out, xd::StringAttrOp &op) {
260+
b.add<xd::cpp::StringAttrOp>(
261+
[](raw_ostream &out, xd::cpp::StringAttrOp &op) {
262262
out << "visiting StringAttrOp: " << op.getVal() << '\n';
263263
});
264264
b.add<ReturnInst>([](raw_ostream &out, ReturnInst &ret) {
@@ -270,8 +270,8 @@ template <bool rpot> const Visitor<VisitorContainer> &getExampleVisitor() {
270270
});
271271
});
272272
b.nest<VisitorInnermost>([](VisitorBuilder<VisitorInnermost> &b) {
273-
b.add<xd::ITruncOp>([](VisitorInnermost &inner,
274-
xd::ITruncOp &op) { inner.counter++; });
273+
b.add<xd::cpp::ITruncOp>([](VisitorInnermost &inner,
274+
xd::cpp::ITruncOp &op) { inner.counter++; });
275275
});
276276
})
277277
.setStrategy(rpot ? VisitorStrategy::ReversePostOrder
@@ -295,7 +295,7 @@ int main(int argc, char **argv) {
295295
llvm::cl::ParseCommandLineOptions(argc, argv);
296296

297297
LLVMContext context;
298-
auto dialectContext = DialectContext::make<xd::ExampleDialect>(context);
298+
auto dialectContext = DialectContext::make<xd::cpp::ExampleDialect>(context);
299299

300300
if (g_action == Action::Build) {
301301
auto module = createModuleExample(context);

lib/TableGen/GenDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void llvm_dialects::genDialectDefs(raw_ostream& out, RecordKeeper& records) {
316316
}
317317
318318
bool $Dialect::isDialectOp(::llvm::StringRef funcName) {
319-
return funcName.starts_with("$namespace.");
319+
return funcName.starts_with("$dialect.");
320320
}
321321
322322
::llvm_dialects::Dialect* $Dialect::make(::llvm::LLVMContext& context) {

0 commit comments

Comments
 (0)