Skip to content

Commit

Permalink
Add instName conflict to ExampleDialect, addressed review
Browse files Browse the repository at this point in the history
  • Loading branch information
JanRehders-AMD authored and tsymalla-AMD committed Feb 2, 2024
1 parent 90c2b4e commit 7e12cc9
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 1 deletion.
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, "try.conflict", [WillReturn]> {
let results = (outs);
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, "try.conflict", [WillReturn]> {
let results = (outs);
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, "try.conflict.type.suffix", [WillReturn]> {
let results = (outs);
let arguments = (ins varargs:$instName_0);

let summary = "demonstrate how name conflict will be avoided";
let description = [{
Like InstNameConflictOp but with varargs
}];
}
241 changes: 241 additions & 0 deletions test/example/generated/ExampleDialect.cpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ namespace xd {
state.setError();
});

builder.add<InstNameConflictDoubleOp>([](::llvm_dialects::VerifierState &state, InstNameConflictDoubleOp &op) {
if (!op.verifier(state.out()))
state.setError();
});

builder.add<InstNameConflictOp>([](::llvm_dialects::VerifierState &state, InstNameConflictOp &op) {
if (!op.verifier(state.out()))
state.setError();
});

builder.add<InstNameConflictVarargsOp>([](::llvm_dialects::VerifierState &state, InstNameConflictVarargsOp &op) {
if (!op.verifier(state.out()))
state.setError();
});

builder.add<ReadOp>([](::llvm_dialects::VerifierState &state, ReadOp &op) {
if (!op.verifier(state.out()))
state.setError();
Expand Down Expand Up @@ -157,6 +172,11 @@ attrBuilder.addAttribute(::llvm::Attribute::NoUnwind);
attrBuilder.addMemoryAttr(::llvm::MemoryEffects(::llvm::MemoryEffects::Location::InaccessibleMem, ::llvm::ModRefInfo::ModRef));
m_attributeLists[3] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder);
}
{
::llvm::AttrBuilder attrBuilder{context};
attrBuilder.addAttribute(::llvm::Attribute::WillReturn);
m_attributeLists[4] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder);
}
}

XdHandleType* XdHandleType::get(::llvm::LLVMContext & ctx) {
Expand Down Expand Up @@ -1166,6 +1186,203 @@ index



const ::llvm::StringLiteral InstNameConflictDoubleOp::s_name{"xd.try.conflict"};

InstNameConflictDoubleOp* InstNameConflictDoubleOp::create(llvm_dialects::Builder& b, ::llvm::Value * instName, ::llvm::Value * instName_0, const llvm::Twine &instName_1) {
::llvm::LLVMContext& context = b.getContext();
(void)context;
::llvm::Module& module = *b.GetInsertBlock()->getModule();


const ::llvm::AttributeList attrs
= ExampleDialect::get(context).getAttributeList(4);
auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true);

auto fn = module.getOrInsertFunction(s_name, fnType, attrs);
::llvm::SmallString<32> newName;
for (unsigned i = 0; !::llvm::isa<::llvm::Function>(fn.getCallee()) ||
::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() != fn.getFunctionType(); i++) {
// If a function with the same name but a different types already exists,
// we get a bitcast of a function or a function with the wrong type.
// Try new names until we get one with the correct type.
newName = "";
::llvm::raw_svector_ostream newNameStream(newName);
newNameStream << s_name << "_" << i;
fn = module.getOrInsertFunction(newNameStream.str(), fnType, attrs);
}
assert(::llvm::isa<::llvm::Function>(fn.getCallee()));
assert(fn.getFunctionType() == fnType);
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());

::llvm::SmallVector<::llvm::Value*, 2> args = {
instName,
instName_0
};

return ::llvm::cast<InstNameConflictDoubleOp>(b.CreateCall(fn, args, instName_1));
}


bool InstNameConflictDoubleOp::verifier(::llvm::raw_ostream &errs) {
::llvm::LLVMContext &context = getModule()->getContext();
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 2) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 2\n";
return false;
}
::llvm::Type * const instNameType = getInstName()->getType();
(void)instNameType;
::llvm::Type * const instName_0Type = getInstName_0()->getType();
(void)instName_0Type;
return true;
}


::llvm::Value * InstNameConflictDoubleOp::getInstName() {
return getArgOperand(0);
}

void InstNameConflictDoubleOp::setInstName(::llvm::Value * instName) {
setArgOperand(0, instName);
}
::llvm::Value * InstNameConflictDoubleOp::getInstName_0() {
return getArgOperand(1);
}

void InstNameConflictDoubleOp::setInstName_0(::llvm::Value * instName_0) {
setArgOperand(1, instName_0);
}



const ::llvm::StringLiteral InstNameConflictOp::s_name{"xd.try.conflict"};

InstNameConflictOp* InstNameConflictOp::create(llvm_dialects::Builder& b, ::llvm::Value * instName, const llvm::Twine &instName_0) {
::llvm::LLVMContext& context = b.getContext();
(void)context;
::llvm::Module& module = *b.GetInsertBlock()->getModule();


const ::llvm::AttributeList attrs
= ExampleDialect::get(context).getAttributeList(4);
auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true);

auto fn = module.getOrInsertFunction(s_name, fnType, attrs);
::llvm::SmallString<32> newName;
for (unsigned i = 0; !::llvm::isa<::llvm::Function>(fn.getCallee()) ||
::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() != fn.getFunctionType(); i++) {
// If a function with the same name but a different types already exists,
// we get a bitcast of a function or a function with the wrong type.
// Try new names until we get one with the correct type.
newName = "";
::llvm::raw_svector_ostream newNameStream(newName);
newNameStream << s_name << "_" << i;
fn = module.getOrInsertFunction(newNameStream.str(), fnType, attrs);
}
assert(::llvm::isa<::llvm::Function>(fn.getCallee()));
assert(fn.getFunctionType() == fnType);
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());

::llvm::SmallVector<::llvm::Value*, 1> args = {
instName
};

return ::llvm::cast<InstNameConflictOp>(b.CreateCall(fn, args, instName_0));
}


bool InstNameConflictOp::verifier(::llvm::raw_ostream &errs) {
::llvm::LLVMContext &context = getModule()->getContext();
(void)context;

using ::llvm_dialects::printable;

if (arg_size() != 1) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected 1\n";
return false;
}
::llvm::Type * const instNameType = getInstName()->getType();
(void)instNameType;
return true;
}


::llvm::Value * InstNameConflictOp::getInstName() {
return getArgOperand(0);
}

void InstNameConflictOp::setInstName(::llvm::Value * instName) {
setArgOperand(0, instName);
}



const ::llvm::StringLiteral InstNameConflictVarargsOp::s_name{"xd.try.conflict.type.suffix"};

InstNameConflictVarargsOp* InstNameConflictVarargsOp::create(llvm_dialects::Builder& b, ::llvm::ArrayRef<::llvm::Value *> instName_0, const llvm::Twine &instName) {
::llvm::LLVMContext& context = b.getContext();
(void)context;
::llvm::Module& module = *b.GetInsertBlock()->getModule();


const ::llvm::AttributeList attrs
= ExampleDialect::get(context).getAttributeList(4);
auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true);

auto fn = module.getOrInsertFunction(s_name, fnType, attrs);
::llvm::SmallString<32> newName;
for (unsigned i = 0; !::llvm::isa<::llvm::Function>(fn.getCallee()) ||
::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() != fn.getFunctionType(); i++) {
// If a function with the same name but a different types already exists,
// we get a bitcast of a function or a function with the wrong type.
// Try new names until we get one with the correct type.
newName = "";
::llvm::raw_svector_ostream newNameStream(newName);
newNameStream << s_name << "_" << i;
fn = module.getOrInsertFunction(newNameStream.str(), fnType, attrs);
}
assert(::llvm::isa<::llvm::Function>(fn.getCallee()));
assert(fn.getFunctionType() == fnType);
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());

::llvm::SmallVector<::llvm::Value*, 0> args = {

};

args.append(instName_0.begin(), instName_0.end());

return ::llvm::cast<InstNameConflictVarargsOp>(b.CreateCall(fn, args, instName));
}


bool InstNameConflictVarargsOp::verifier(::llvm::raw_ostream &errs) {
::llvm::LLVMContext &context = getModule()->getContext();
(void)context;

using ::llvm_dialects::printable;

if (arg_size() < 0) {
errs << " wrong number of arguments: " << arg_size()
<< ", expected at least 0\n";
return false;
}
return true;
}


::llvm::iterator_range<::llvm::User::value_op_iterator> InstNameConflictVarargsOp::getInstName_0() {
return ::llvm::make_range(
value_op_iterator(arg_begin() + 0),
value_op_iterator(arg_end()));
}



const ::llvm::StringLiteral ReadOp::s_name{"xd.read"};

ReadOp* ReadOp::create(llvm_dialects::Builder& b, ::llvm::Type* dataType, const llvm::Twine &instName) {
Expand Down Expand Up @@ -1891,6 +2108,30 @@ data
}


template <>
const ::llvm_dialects::OpDescription &
::llvm_dialects::OpDescription::get<xd::InstNameConflictDoubleOp>() {
static const ::llvm_dialects::OpDescription desc{false, "xd.try.conflict"};
return desc;
}


template <>
const ::llvm_dialects::OpDescription &
::llvm_dialects::OpDescription::get<xd::InstNameConflictOp>() {
static const ::llvm_dialects::OpDescription desc{false, "xd.try.conflict"};
return desc;
}


template <>
const ::llvm_dialects::OpDescription &
::llvm_dialects::OpDescription::get<xd::InstNameConflictVarargsOp>() {
static const ::llvm_dialects::OpDescription desc{false, "xd.try.conflict.type.suffix"};
return desc;
}


template <>
const ::llvm_dialects::OpDescription &
::llvm_dialects::OpDescription::get<xd::ReadOp>() {
Expand Down
68 changes: 67 additions & 1 deletion test/example/generated/ExampleDialect.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace xd {
}

private:
::std::array<::llvm::AttributeList, 4> m_attributeLists;
::std::array<::llvm::AttributeList, 5> m_attributeLists;
};

class XdHandleType : public ::llvm::TargetExtType {
Expand Down Expand Up @@ -296,6 +296,72 @@ bool verifier(::llvm::raw_ostream &errs);
::llvm::Value * getResult();


};

class InstNameConflictDoubleOp : public ::llvm::CallInst {
static const ::llvm::StringLiteral s_name; //{"xd.try.conflict"};

public:
static bool classof(const ::llvm::CallInst* i) {
return ::llvm_dialects::detail::isSimpleOperation(i, s_name);
}
static bool classof(const ::llvm::Value* v) {
return ::llvm::isa<::llvm::CallInst>(v) &&
classof(::llvm::cast<::llvm::CallInst>(v));
}
static InstNameConflictDoubleOp* create(::llvm_dialects::Builder& b, ::llvm::Value * instName, ::llvm::Value * instName_0, const llvm::Twine &instName_1 = "");

bool verifier(::llvm::raw_ostream &errs);

::llvm::Value * getInstName();
void setInstName(::llvm::Value * instName);
::llvm::Value * getInstName_0();
void setInstName_0(::llvm::Value * instName_0);



};

class InstNameConflictOp : public ::llvm::CallInst {
static const ::llvm::StringLiteral s_name; //{"xd.try.conflict"};

public:
static bool classof(const ::llvm::CallInst* i) {
return ::llvm_dialects::detail::isSimpleOperation(i, s_name);
}
static bool classof(const ::llvm::Value* v) {
return ::llvm::isa<::llvm::CallInst>(v) &&
classof(::llvm::cast<::llvm::CallInst>(v));
}
static InstNameConflictOp* create(::llvm_dialects::Builder& b, ::llvm::Value * instName, const llvm::Twine &instName_0 = "");

bool verifier(::llvm::raw_ostream &errs);

::llvm::Value * getInstName();
void setInstName(::llvm::Value * instName);



};

class InstNameConflictVarargsOp : public ::llvm::CallInst {
static const ::llvm::StringLiteral s_name; //{"xd.try.conflict.type.suffix"};

public:
static bool classof(const ::llvm::CallInst* i) {
return ::llvm_dialects::detail::isSimpleOperation(i, s_name);
}
static bool classof(const ::llvm::Value* v) {
return ::llvm::isa<::llvm::CallInst>(v) &&
classof(::llvm::cast<::llvm::CallInst>(v));
}
static InstNameConflictVarargsOp* create(::llvm_dialects::Builder& b, ::llvm::ArrayRef<::llvm::Value *> instName_0, const llvm::Twine &instName = "");

bool verifier(::llvm::raw_ostream &errs);

::llvm::iterator_range<::llvm::User::value_op_iterator> getInstName_0();


};

class ReadOp : public ::llvm::CallInst {
Expand Down

0 comments on commit 7e12cc9

Please sign in to comment.