Skip to content

Commit 4e785a2

Browse files
Thomas Symallatsymalla-AMD
authored andcommitted
Add Dialect::isDialectOp helper function.
We sometimes want to check if a given call instruction is prefixed with the cpp namespace assigned to the dialect. Instead of doing that manually, add a static dialect function that does this check.
1 parent ed4b46e commit 4e785a2

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

example/ExampleMain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ void createFunctionExample(Module &module, const Twine &name) {
117117
p2->setName("p2");
118118
b.create<xd::WriteOp>(p2);
119119

120+
assert(xd::ExampleDialect::isDialectOp(*cast<CallInst>(p2)));
121+
120122
SmallVector<Value *> varArgs;
121123
varArgs.push_back(p1);
122124
varArgs.push_back(p2);

lib/TableGen/GenDialect.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class Builder;
107107
108108
public:
109109
static Key& getKey();
110+
static bool isDialectOp(::llvm::CallInst& op);
111+
static bool isDialectOp(::llvm::Function& func);
112+
static bool isDialectOp(::llvm::StringRef funcName);
110113
111114
private:
112115
$Dialect(::llvm::LLVMContext& context);
@@ -298,6 +301,18 @@ void llvm_dialects::genDialectDefs(raw_ostream& out, RecordKeeper& records) {
298301
return s_key;
299302
}
300303
304+
bool $Dialect::isDialectOp(::llvm::CallInst& op) {
305+
return isDialectOp(op.getCalledFunction()->getName());
306+
}
307+
308+
bool $Dialect::isDialectOp(::llvm::Function& func) {
309+
return isDialectOp(func.getName());
310+
}
311+
312+
bool $Dialect::isDialectOp(::llvm::StringRef funcName) {
313+
return funcName.starts_with("$namespace.");
314+
}
315+
301316
::llvm_dialects::Dialect* $Dialect::make(::llvm::LLVMContext& context) {
302317
$0
303318
return new $Dialect(context);

test/example/generated/ExampleDialect.cpp.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ namespace xd {
3030
return s_key;
3131
}
3232

33+
bool ExampleDialect::isDialectOp(::llvm::CallInst& op) {
34+
return isDialectOp(op.getCalledFunction()->getName());
35+
}
36+
37+
bool ExampleDialect::isDialectOp(::llvm::Function& func) {
38+
return isDialectOp(func.getName());
39+
}
40+
41+
bool ExampleDialect::isDialectOp(::llvm::StringRef funcName) {
42+
return funcName.starts_with("xd.");
43+
}
44+
3345
::llvm_dialects::Dialect* ExampleDialect::make(::llvm::LLVMContext& context) {
3446

3547
auto verifierBuild = [](::llvm_dialects::VisitorBuilder<::llvm_dialects::VerifierState> &builder) {

test/example/generated/ExampleDialect.h.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace xd {
3232

3333
public:
3434
static Key& getKey();
35+
static bool isDialectOp(::llvm::CallInst& op);
36+
static bool isDialectOp(::llvm::Function& func);
37+
static bool isDialectOp(::llvm::StringRef funcName);
3538

3639
private:
3740
ExampleDialect(::llvm::LLVMContext& context);

0 commit comments

Comments
 (0)