Skip to content

Commit 7e12cc9

Browse files
JanRehders-AMDtsymalla-AMD
authored andcommitted
Add instName conflict to ExampleDialect, addressed review
1 parent 90c2b4e commit 7e12cc9

File tree

3 files changed

+341
-1
lines changed

3 files changed

+341
-1
lines changed

example/ExampleDialect.td

+33
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,36 @@ def HandleGetOp : ExampleOp<"handle.get", [Memory<[]>, NoUnwind, WillReturn]> {
268268
Use a dialect type without type arguments.
269269
}];
270270
}
271+
272+
def InstNameConflictOp : Op<ExampleDialect, "try.conflict", [WillReturn]> {
273+
let results = (outs);
274+
let arguments = (ins value:$instName);
275+
276+
let summary = "demonstrate how name conflict will be avoided";
277+
let description = [{
278+
The builder accepts an additional argument to set the name of the created
279+
value like IRBuilder methods. This op produces a conflict so the parameter
280+
will be renamed.
281+
}];
282+
}
283+
284+
def InstNameConflictDoubleOp : Op<ExampleDialect, "try.conflict", [WillReturn]> {
285+
let results = (outs);
286+
let arguments = (ins value:$instName, value:$instName_0);
287+
288+
let summary = "demonstrate how name conflict will be avoided";
289+
let description = [{
290+
Like InstNameConflictOp but this has a second parameter named like the
291+
dialect compiler's first choice
292+
}];
293+
}
294+
295+
def InstNameConflictVarargsOp : Op<ExampleDialect, "try.conflict.type.suffix", [WillReturn]> {
296+
let results = (outs);
297+
let arguments = (ins varargs:$instName_0);
298+
299+
let summary = "demonstrate how name conflict will be avoided";
300+
let description = [{
301+
Like InstNameConflictOp but with varargs
302+
}];
303+
}

test/example/generated/ExampleDialect.cpp.inc

+241
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ namespace xd {
7474
state.setError();
7575
});
7676

77+
builder.add<InstNameConflictDoubleOp>([](::llvm_dialects::VerifierState &state, InstNameConflictDoubleOp &op) {
78+
if (!op.verifier(state.out()))
79+
state.setError();
80+
});
81+
82+
builder.add<InstNameConflictOp>([](::llvm_dialects::VerifierState &state, InstNameConflictOp &op) {
83+
if (!op.verifier(state.out()))
84+
state.setError();
85+
});
86+
87+
builder.add<InstNameConflictVarargsOp>([](::llvm_dialects::VerifierState &state, InstNameConflictVarargsOp &op) {
88+
if (!op.verifier(state.out()))
89+
state.setError();
90+
});
91+
7792
builder.add<ReadOp>([](::llvm_dialects::VerifierState &state, ReadOp &op) {
7893
if (!op.verifier(state.out()))
7994
state.setError();
@@ -157,6 +172,11 @@ attrBuilder.addAttribute(::llvm::Attribute::NoUnwind);
157172
attrBuilder.addMemoryAttr(::llvm::MemoryEffects(::llvm::MemoryEffects::Location::InaccessibleMem, ::llvm::ModRefInfo::ModRef));
158173
m_attributeLists[3] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder);
159174
}
175+
{
176+
::llvm::AttrBuilder attrBuilder{context};
177+
attrBuilder.addAttribute(::llvm::Attribute::WillReturn);
178+
m_attributeLists[4] = ::llvm::AttributeList::get(context, ::llvm::AttributeList::FunctionIndex, attrBuilder);
179+
}
160180
}
161181

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

11671187

11681188

1189+
const ::llvm::StringLiteral InstNameConflictDoubleOp::s_name{"xd.try.conflict"};
1190+
1191+
InstNameConflictDoubleOp* InstNameConflictDoubleOp::create(llvm_dialects::Builder& b, ::llvm::Value * instName, ::llvm::Value * instName_0, const llvm::Twine &instName_1) {
1192+
::llvm::LLVMContext& context = b.getContext();
1193+
(void)context;
1194+
::llvm::Module& module = *b.GetInsertBlock()->getModule();
1195+
1196+
1197+
const ::llvm::AttributeList attrs
1198+
= ExampleDialect::get(context).getAttributeList(4);
1199+
auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true);
1200+
1201+
auto fn = module.getOrInsertFunction(s_name, fnType, attrs);
1202+
::llvm::SmallString<32> newName;
1203+
for (unsigned i = 0; !::llvm::isa<::llvm::Function>(fn.getCallee()) ||
1204+
::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() != fn.getFunctionType(); i++) {
1205+
// If a function with the same name but a different types already exists,
1206+
// we get a bitcast of a function or a function with the wrong type.
1207+
// Try new names until we get one with the correct type.
1208+
newName = "";
1209+
::llvm::raw_svector_ostream newNameStream(newName);
1210+
newNameStream << s_name << "_" << i;
1211+
fn = module.getOrInsertFunction(newNameStream.str(), fnType, attrs);
1212+
}
1213+
assert(::llvm::isa<::llvm::Function>(fn.getCallee()));
1214+
assert(fn.getFunctionType() == fnType);
1215+
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());
1216+
1217+
::llvm::SmallVector<::llvm::Value*, 2> args = {
1218+
instName,
1219+
instName_0
1220+
};
1221+
1222+
return ::llvm::cast<InstNameConflictDoubleOp>(b.CreateCall(fn, args, instName_1));
1223+
}
1224+
1225+
1226+
bool InstNameConflictDoubleOp::verifier(::llvm::raw_ostream &errs) {
1227+
::llvm::LLVMContext &context = getModule()->getContext();
1228+
(void)context;
1229+
1230+
using ::llvm_dialects::printable;
1231+
1232+
if (arg_size() != 2) {
1233+
errs << " wrong number of arguments: " << arg_size()
1234+
<< ", expected 2\n";
1235+
return false;
1236+
}
1237+
::llvm::Type * const instNameType = getInstName()->getType();
1238+
(void)instNameType;
1239+
::llvm::Type * const instName_0Type = getInstName_0()->getType();
1240+
(void)instName_0Type;
1241+
return true;
1242+
}
1243+
1244+
1245+
::llvm::Value * InstNameConflictDoubleOp::getInstName() {
1246+
return getArgOperand(0);
1247+
}
1248+
1249+
void InstNameConflictDoubleOp::setInstName(::llvm::Value * instName) {
1250+
setArgOperand(0, instName);
1251+
}
1252+
::llvm::Value * InstNameConflictDoubleOp::getInstName_0() {
1253+
return getArgOperand(1);
1254+
}
1255+
1256+
void InstNameConflictDoubleOp::setInstName_0(::llvm::Value * instName_0) {
1257+
setArgOperand(1, instName_0);
1258+
}
1259+
1260+
1261+
1262+
const ::llvm::StringLiteral InstNameConflictOp::s_name{"xd.try.conflict"};
1263+
1264+
InstNameConflictOp* InstNameConflictOp::create(llvm_dialects::Builder& b, ::llvm::Value * instName, const llvm::Twine &instName_0) {
1265+
::llvm::LLVMContext& context = b.getContext();
1266+
(void)context;
1267+
::llvm::Module& module = *b.GetInsertBlock()->getModule();
1268+
1269+
1270+
const ::llvm::AttributeList attrs
1271+
= ExampleDialect::get(context).getAttributeList(4);
1272+
auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true);
1273+
1274+
auto fn = module.getOrInsertFunction(s_name, fnType, attrs);
1275+
::llvm::SmallString<32> newName;
1276+
for (unsigned i = 0; !::llvm::isa<::llvm::Function>(fn.getCallee()) ||
1277+
::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() != fn.getFunctionType(); i++) {
1278+
// If a function with the same name but a different types already exists,
1279+
// we get a bitcast of a function or a function with the wrong type.
1280+
// Try new names until we get one with the correct type.
1281+
newName = "";
1282+
::llvm::raw_svector_ostream newNameStream(newName);
1283+
newNameStream << s_name << "_" << i;
1284+
fn = module.getOrInsertFunction(newNameStream.str(), fnType, attrs);
1285+
}
1286+
assert(::llvm::isa<::llvm::Function>(fn.getCallee()));
1287+
assert(fn.getFunctionType() == fnType);
1288+
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());
1289+
1290+
::llvm::SmallVector<::llvm::Value*, 1> args = {
1291+
instName
1292+
};
1293+
1294+
return ::llvm::cast<InstNameConflictOp>(b.CreateCall(fn, args, instName_0));
1295+
}
1296+
1297+
1298+
bool InstNameConflictOp::verifier(::llvm::raw_ostream &errs) {
1299+
::llvm::LLVMContext &context = getModule()->getContext();
1300+
(void)context;
1301+
1302+
using ::llvm_dialects::printable;
1303+
1304+
if (arg_size() != 1) {
1305+
errs << " wrong number of arguments: " << arg_size()
1306+
<< ", expected 1\n";
1307+
return false;
1308+
}
1309+
::llvm::Type * const instNameType = getInstName()->getType();
1310+
(void)instNameType;
1311+
return true;
1312+
}
1313+
1314+
1315+
::llvm::Value * InstNameConflictOp::getInstName() {
1316+
return getArgOperand(0);
1317+
}
1318+
1319+
void InstNameConflictOp::setInstName(::llvm::Value * instName) {
1320+
setArgOperand(0, instName);
1321+
}
1322+
1323+
1324+
1325+
const ::llvm::StringLiteral InstNameConflictVarargsOp::s_name{"xd.try.conflict.type.suffix"};
1326+
1327+
InstNameConflictVarargsOp* InstNameConflictVarargsOp::create(llvm_dialects::Builder& b, ::llvm::ArrayRef<::llvm::Value *> instName_0, const llvm::Twine &instName) {
1328+
::llvm::LLVMContext& context = b.getContext();
1329+
(void)context;
1330+
::llvm::Module& module = *b.GetInsertBlock()->getModule();
1331+
1332+
1333+
const ::llvm::AttributeList attrs
1334+
= ExampleDialect::get(context).getAttributeList(4);
1335+
auto fnType = ::llvm::FunctionType::get(::llvm::Type::getVoidTy(context), true);
1336+
1337+
auto fn = module.getOrInsertFunction(s_name, fnType, attrs);
1338+
::llvm::SmallString<32> newName;
1339+
for (unsigned i = 0; !::llvm::isa<::llvm::Function>(fn.getCallee()) ||
1340+
::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() != fn.getFunctionType(); i++) {
1341+
// If a function with the same name but a different types already exists,
1342+
// we get a bitcast of a function or a function with the wrong type.
1343+
// Try new names until we get one with the correct type.
1344+
newName = "";
1345+
::llvm::raw_svector_ostream newNameStream(newName);
1346+
newNameStream << s_name << "_" << i;
1347+
fn = module.getOrInsertFunction(newNameStream.str(), fnType, attrs);
1348+
}
1349+
assert(::llvm::isa<::llvm::Function>(fn.getCallee()));
1350+
assert(fn.getFunctionType() == fnType);
1351+
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());
1352+
1353+
::llvm::SmallVector<::llvm::Value*, 0> args = {
1354+
1355+
};
1356+
1357+
args.append(instName_0.begin(), instName_0.end());
1358+
1359+
return ::llvm::cast<InstNameConflictVarargsOp>(b.CreateCall(fn, args, instName));
1360+
}
1361+
1362+
1363+
bool InstNameConflictVarargsOp::verifier(::llvm::raw_ostream &errs) {
1364+
::llvm::LLVMContext &context = getModule()->getContext();
1365+
(void)context;
1366+
1367+
using ::llvm_dialects::printable;
1368+
1369+
if (arg_size() < 0) {
1370+
errs << " wrong number of arguments: " << arg_size()
1371+
<< ", expected at least 0\n";
1372+
return false;
1373+
}
1374+
return true;
1375+
}
1376+
1377+
1378+
::llvm::iterator_range<::llvm::User::value_op_iterator> InstNameConflictVarargsOp::getInstName_0() {
1379+
return ::llvm::make_range(
1380+
value_op_iterator(arg_begin() + 0),
1381+
value_op_iterator(arg_end()));
1382+
}
1383+
1384+
1385+
11691386
const ::llvm::StringLiteral ReadOp::s_name{"xd.read"};
11701387

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

18932110

2111+
template <>
2112+
const ::llvm_dialects::OpDescription &
2113+
::llvm_dialects::OpDescription::get<xd::InstNameConflictDoubleOp>() {
2114+
static const ::llvm_dialects::OpDescription desc{false, "xd.try.conflict"};
2115+
return desc;
2116+
}
2117+
2118+
2119+
template <>
2120+
const ::llvm_dialects::OpDescription &
2121+
::llvm_dialects::OpDescription::get<xd::InstNameConflictOp>() {
2122+
static const ::llvm_dialects::OpDescription desc{false, "xd.try.conflict"};
2123+
return desc;
2124+
}
2125+
2126+
2127+
template <>
2128+
const ::llvm_dialects::OpDescription &
2129+
::llvm_dialects::OpDescription::get<xd::InstNameConflictVarargsOp>() {
2130+
static const ::llvm_dialects::OpDescription desc{false, "xd.try.conflict.type.suffix"};
2131+
return desc;
2132+
}
2133+
2134+
18942135
template <>
18952136
const ::llvm_dialects::OpDescription &
18962137
::llvm_dialects::OpDescription::get<xd::ReadOp>() {

test/example/generated/ExampleDialect.h.inc

+67-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace xd {
4444
}
4545

4646
private:
47-
::std::array<::llvm::AttributeList, 4> m_attributeLists;
47+
::std::array<::llvm::AttributeList, 5> m_attributeLists;
4848
};
4949

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

298298

299+
};
300+
301+
class InstNameConflictDoubleOp : public ::llvm::CallInst {
302+
static const ::llvm::StringLiteral s_name; //{"xd.try.conflict"};
303+
304+
public:
305+
static bool classof(const ::llvm::CallInst* i) {
306+
return ::llvm_dialects::detail::isSimpleOperation(i, s_name);
307+
}
308+
static bool classof(const ::llvm::Value* v) {
309+
return ::llvm::isa<::llvm::CallInst>(v) &&
310+
classof(::llvm::cast<::llvm::CallInst>(v));
311+
}
312+
static InstNameConflictDoubleOp* create(::llvm_dialects::Builder& b, ::llvm::Value * instName, ::llvm::Value * instName_0, const llvm::Twine &instName_1 = "");
313+
314+
bool verifier(::llvm::raw_ostream &errs);
315+
316+
::llvm::Value * getInstName();
317+
void setInstName(::llvm::Value * instName);
318+
::llvm::Value * getInstName_0();
319+
void setInstName_0(::llvm::Value * instName_0);
320+
321+
322+
323+
};
324+
325+
class InstNameConflictOp : public ::llvm::CallInst {
326+
static const ::llvm::StringLiteral s_name; //{"xd.try.conflict"};
327+
328+
public:
329+
static bool classof(const ::llvm::CallInst* i) {
330+
return ::llvm_dialects::detail::isSimpleOperation(i, s_name);
331+
}
332+
static bool classof(const ::llvm::Value* v) {
333+
return ::llvm::isa<::llvm::CallInst>(v) &&
334+
classof(::llvm::cast<::llvm::CallInst>(v));
335+
}
336+
static InstNameConflictOp* create(::llvm_dialects::Builder& b, ::llvm::Value * instName, const llvm::Twine &instName_0 = "");
337+
338+
bool verifier(::llvm::raw_ostream &errs);
339+
340+
::llvm::Value * getInstName();
341+
void setInstName(::llvm::Value * instName);
342+
343+
344+
345+
};
346+
347+
class InstNameConflictVarargsOp : public ::llvm::CallInst {
348+
static const ::llvm::StringLiteral s_name; //{"xd.try.conflict.type.suffix"};
349+
350+
public:
351+
static bool classof(const ::llvm::CallInst* i) {
352+
return ::llvm_dialects::detail::isSimpleOperation(i, s_name);
353+
}
354+
static bool classof(const ::llvm::Value* v) {
355+
return ::llvm::isa<::llvm::CallInst>(v) &&
356+
classof(::llvm::cast<::llvm::CallInst>(v));
357+
}
358+
static InstNameConflictVarargsOp* create(::llvm_dialects::Builder& b, ::llvm::ArrayRef<::llvm::Value *> instName_0, const llvm::Twine &instName = "");
359+
360+
bool verifier(::llvm::raw_ostream &errs);
361+
362+
::llvm::iterator_range<::llvm::User::value_op_iterator> getInstName_0();
363+
364+
299365
};
300366

301367
class ReadOp : public ::llvm::CallInst {

0 commit comments

Comments
 (0)