Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Op's create methods support passing an instruction name #80

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions example/ExampleDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,36 @@ def HandleGetOp : ExampleOp<"handle.get", [Memory<[]>, NoUnwind, WillReturn]> {
Use a dialect type without type arguments.
}];
}

def InstNameConflictOp : Op<ExampleDialect, "inst.name.conflict", [WillReturn]> {
let results = (outs I32:$result);
let arguments = (ins value:$instName);

let summary = "demonstrate how name conflict will be avoided";
let description = [{
The builder accepts an additional argument to set the name of the created
value like IRBuilder methods. This op produces a conflict so the parameter
will be renamed.
}];
}

def InstNameConflictDoubleOp : Op<ExampleDialect, "inst.name.conflict.double", [WillReturn]> {
let results = (outs I32:$result);
let arguments = (ins value:$instName, value:$instName_0);

let summary = "demonstrate how name conflict will be avoided";
let description = [{
Like InstNameConflictOp but this has a second parameter named like the
dialect compiler's first choice
}];
}

def InstNameConflictVarargsOp : Op<ExampleDialect, "inst.name.conflict.varargs", [WillReturn]> {
let results = (outs I32:$result);
let arguments = (ins varargs:$instName_0);

let summary = "demonstrate how name conflict will be avoided";
let description = [{
Like InstNameConflictOp but with varargs
}];
}
13 changes: 13 additions & 0 deletions example/ExampleMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ void createFunctionExample(Module &module, const Twine &name) {

useUnnamedStructTypes(b);

b.create<xd::HandleGetOp>("name.of.llvm.value");
b.create<xd::InstNameConflictOp>(b.getInt32(1));
b.create<xd::InstNameConflictOp>(b.getInt32(1), "name.foo");
b.create<xd::InstNameConflictDoubleOp>(b.getInt32(1), b.getInt32(2));
b.create<xd::InstNameConflictDoubleOp>(b.getInt32(1), b.getInt32(2), "bar");
SmallVector<Value *> moreVarArgs = varArgs;
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs);
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs, "two.varargs");
moreVarArgs.push_back(b.getInt32(3));
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs, "three.varargs");
moreVarArgs.push_back(b.getInt32(4));
b.create<xd::InstNameConflictVarargsOp>(moreVarArgs, "four.varargs");

b.CreateRetVoid();
}

Expand Down
1 change: 1 addition & 0 deletions include/llvm-dialects/TableGen/Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class BuilderMethod {
std::string m_context;
std::string m_builder;
std::vector<Arg> m_arguments;
std::string m_instName;
unsigned m_beginOpArguments = 0;

std::string m_prelude;
Expand Down
13 changes: 9 additions & 4 deletions lib/TableGen/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ bool Operation::parse(raw_ostream &errs, GenDialectsContext *context,
convertToCamelFromSnakeCase(opArg.name)));
}

builder.m_instName = builder.m_symbolTable.chooseName("instName");
builder.m_context = builder.m_symbolTable.chooseName("context");
builder.m_builder = builder.m_symbolTable.chooseName({"b", "builder"});

Expand Down Expand Up @@ -548,12 +549,14 @@ void Operation::emitVerifierMethod(llvm::raw_ostream &out,
void BuilderMethod::emitDeclaration(raw_ostream &out, FmtContext &fmt) const {
FmtContextScope scope{fmt};
fmt.withBuilder(m_builder);
assert(m_instName.size() > 0);
fmt.addSubst("_instname", m_instName);

out << tgfmt("static $_op* create(::llvm_dialects::Builder& $_builder", &fmt);
for (const auto &builderArg : m_arguments) {
out << ", " << builderArg.cppType << " " << builderArg.name;
}
out << ");\n";
out << tgfmt(", const llvm::Twine &$_instname = \"\");\n", &fmt);
}

void BuilderMethod::emitDefinition(raw_ostream &out, FmtContext &fmt,
Expand All @@ -567,12 +570,14 @@ void BuilderMethod::emitDefinition(raw_ostream &out, FmtContext &fmt,
fmt.withBuilder(m_builder);
fmt.withContext(m_context);
fmt.addSubst("_module", symbols.chooseName("module"));
assert(m_instName.size() > 0);
fmt.addSubst("_instname", m_instName);

out << tgfmt("$_op* $_op::create(llvm_dialects::Builder& $_builder", &fmt);
for (const auto &builderArg : m_arguments)
out << tgfmt(", $0 $1", &fmt, builderArg.cppType, builderArg.name);

out << tgfmt(R"() {
out << tgfmt(R"(, const llvm::Twine &$_instname) {
::llvm::LLVMContext& $_context = $_builder.getContext();
(void)$_context;
::llvm::Module& $_module = *$_builder.GetInsertBlock()->getModule();
Expand Down Expand Up @@ -711,11 +716,11 @@ void BuilderMethod::emitDefinition(raw_ostream &out, FmtContext &fmt,
out << tgfmt(R"(
};
$varArgInitializer
return ::llvm::cast<$_op>($_builder.CreateCall($fn, $args));
return ::llvm::cast<$_op>($_builder.CreateCall($fn, $args, $_instname));
)",
&fmt);
} else {
out << tgfmt("return ::llvm::cast<$_op>($_builder.CreateCall($fn));\n",
out << tgfmt("return ::llvm::cast<$_op>($_builder.CreateCall($fn, std::nullopt, $_instname));\n",
&fmt);
}

Expand Down
Loading
Loading