Skip to content

Commit

Permalink
Add Dialect::isDialectOp helper function.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thomas Symalla committed Jun 6, 2024
1 parent ed4b46e commit bfdde70
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example/ExampleMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ void createFunctionExample(Module &module, const Twine &name) {
p2->setName("p2");
b.create<xd::WriteOp>(p2);

assert(xd::ExampleDialect::isDialectOp(*cast<CallInst>(p2)));

SmallVector<Value *> varArgs;
varArgs.push_back(p1);
varArgs.push_back(p2);
Expand Down
15 changes: 15 additions & 0 deletions lib/TableGen/GenDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class Builder;
public:
static Key& getKey();
static bool isDialectOp(::llvm::CallInst& op);
static bool isDialectOp(::llvm::Function& func);
static bool isDialectOp(::llvm::StringRef funcName);
private:
$Dialect(::llvm::LLVMContext& context);
Expand Down Expand Up @@ -298,6 +301,18 @@ void llvm_dialects::genDialectDefs(raw_ostream& out, RecordKeeper& records) {
return s_key;
}
bool $Dialect::isDialectOp(::llvm::CallInst& op) {
return isDialectOp(op.getCalledFunction()->getName());
}
bool $Dialect::isDialectOp(::llvm::Function& func) {
return isDialectOp(func.getName());
}
bool $Dialect::isDialectOp(::llvm::StringRef funcName) {
return funcName.starts_with("$namespace.");
}
::llvm_dialects::Dialect* $Dialect::make(::llvm::LLVMContext& context) {
$0
return new $Dialect(context);
Expand Down
12 changes: 12 additions & 0 deletions test/example/generated/ExampleDialect.cpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ namespace xd {
return s_key;
}

bool ExampleDialect::isDialectOp(::llvm::CallInst& op) {
return isDialectOp(op.getCalledFunction()->getName());
}

bool ExampleDialect::isDialectOp(::llvm::Function& func) {
return isDialectOp(func.getName());
}

bool ExampleDialect::isDialectOp(::llvm::StringRef funcName) {
return funcName.starts_with("xd.");
}

::llvm_dialects::Dialect* ExampleDialect::make(::llvm::LLVMContext& context) {

auto verifierBuild = [](::llvm_dialects::VisitorBuilder<::llvm_dialects::VerifierState> &builder) {
Expand Down
3 changes: 3 additions & 0 deletions test/example/generated/ExampleDialect.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace xd {

public:
static Key& getKey();
static bool isDialectOp(::llvm::CallInst& op);
static bool isDialectOp(::llvm::Function& func);
static bool isDialectOp(::llvm::StringRef funcName);

private:
ExampleDialect(::llvm::LLVMContext& context);
Expand Down

0 comments on commit bfdde70

Please sign in to comment.