Skip to content

Commit 90c2b4e

Browse files
JanRehders-AMDtsymalla-AMD
authored andcommitted
Use SymbolTable to avoid name clashes instead relying on convention
1 parent b37db10 commit 90c2b4e

File tree

4 files changed

+61
-55
lines changed

4 files changed

+61
-55
lines changed

include/llvm-dialects/TableGen/Operations.h

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class BuilderMethod {
154154
std::string m_context;
155155
std::string m_builder;
156156
std::vector<Arg> m_arguments;
157+
std::string m_instName;
157158
unsigned m_beginOpArguments = 0;
158159

159160
std::string m_prelude;

lib/TableGen/Operations.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ bool Operation::parse(raw_ostream &errs, GenDialectsContext *context,
379379
convertToCamelFromSnakeCase(opArg.name)));
380380
}
381381

382+
builder.m_instName = builder.m_symbolTable.chooseName("instName");
382383
builder.m_context = builder.m_symbolTable.chooseName("context");
383384
builder.m_builder = builder.m_symbolTable.chooseName({"b", "builder"});
384385

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

552555
out << tgfmt("static $_op* create(::llvm_dialects::Builder& $_builder", &fmt);
553556
for (const auto &builderArg : m_arguments) {
554557
out << ", " << builderArg.cppType << " " << builderArg.name;
555558
}
556-
out << ", const llvm::Twine &inst__name = \"\");\n";
559+
out << tgfmt(", const llvm::Twine &$_instname = \"\");\n", &fmt);
557560
}
558561

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

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

575-
out << tgfmt(R"(, const llvm::Twine &inst__name) {
580+
out << tgfmt(R"(, const llvm::Twine &$_instname) {
576581
::llvm::LLVMContext& $_context = $_builder.getContext();
577582
(void)$_context;
578583
::llvm::Module& $_module = *$_builder.GetInsertBlock()->getModule();
@@ -711,11 +716,11 @@ void BuilderMethod::emitDefinition(raw_ostream &out, FmtContext &fmt,
711716
out << tgfmt(R"(
712717
};
713718
$varArgInitializer
714-
return ::llvm::cast<$_op>($_builder.CreateCall($fn, $args, inst__name));
719+
return ::llvm::cast<$_op>($_builder.CreateCall($fn, $args, $_instname));
715720
)",
716721
&fmt);
717722
} else {
718-
out << tgfmt("return ::llvm::cast<$_op>($_builder.CreateCall($fn, std::nullopt, inst__name));\n",
723+
out << tgfmt("return ::llvm::cast<$_op>($_builder.CreateCall($fn, std::nullopt, $_instname));\n",
719724
&fmt);
720725
}
721726

test/example/generated/ExampleDialect.cpp.inc

+34-34
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ return true;
297297

298298
const ::llvm::StringLiteral Add32Op::s_name{"xd.add32"};
299299

300-
Add32Op* Add32Op::create(llvm_dialects::Builder& b, ::llvm::Value * lhs, ::llvm::Value * rhs, uint32_t extra, const llvm::Twine &inst__name) {
300+
Add32Op* Add32Op::create(llvm_dialects::Builder& b, ::llvm::Value * lhs, ::llvm::Value * rhs, uint32_t extra, const llvm::Twine &instName) {
301301
::llvm::LLVMContext& context = b.getContext();
302302
(void)context;
303303
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -334,7 +334,7 @@ rhs,
334334
::llvm::ConstantInt::get(::llvm::IntegerType::get(context, 32), extra)
335335
};
336336

337-
return ::llvm::cast<Add32Op>(b.CreateCall(fn, args, inst__name));
337+
return ::llvm::cast<Add32Op>(b.CreateCall(fn, args, instName));
338338
}
339339

340340

@@ -419,7 +419,7 @@ uint32_t const extra = getExtra();
419419

420420
const ::llvm::StringLiteral CombineOp::s_name{"xd.combine"};
421421

422-
CombineOp* CombineOp::create(llvm_dialects::Builder& b, ::llvm::Value * lhs, ::llvm::Value * rhs, const llvm::Twine &inst__name) {
422+
CombineOp* CombineOp::create(llvm_dialects::Builder& b, ::llvm::Value * lhs, ::llvm::Value * rhs, const llvm::Twine &instName) {
423423
::llvm::LLVMContext& context = b.getContext();
424424
(void)context;
425425
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -453,7 +453,7 @@ lhs,
453453
rhs
454454
};
455455

456-
return ::llvm::cast<CombineOp>(b.CreateCall(fn, args, inst__name));
456+
return ::llvm::cast<CombineOp>(b.CreateCall(fn, args, instName));
457457
}
458458

459459

@@ -514,7 +514,7 @@ rhs
514514

515515
const ::llvm::StringLiteral ExtractElementOp::s_name{"xd.extractelement"};
516516

517-
ExtractElementOp* ExtractElementOp::create(llvm_dialects::Builder& b, ::llvm::Value * vector, ::llvm::Value * index, const llvm::Twine &inst__name) {
517+
ExtractElementOp* ExtractElementOp::create(llvm_dialects::Builder& b, ::llvm::Value * vector, ::llvm::Value * index, const llvm::Twine &instName) {
518518
::llvm::LLVMContext& context = b.getContext();
519519
(void)context;
520520
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -548,7 +548,7 @@ vector,
548548
index
549549
};
550550

551-
return ::llvm::cast<ExtractElementOp>(b.CreateCall(fn, args, inst__name));
551+
return ::llvm::cast<ExtractElementOp>(b.CreateCall(fn, args, instName));
552552
}
553553

554554

@@ -618,7 +618,7 @@ index
618618

619619
const ::llvm::StringLiteral FromFixedVectorOp::s_name{"xd.fromfixedvector"};
620620

621-
FromFixedVectorOp* FromFixedVectorOp::create(llvm_dialects::Builder& b, ::llvm::Type* resultType, ::llvm::Value * source, const llvm::Twine &inst__name) {
621+
FromFixedVectorOp* FromFixedVectorOp::create(llvm_dialects::Builder& b, ::llvm::Type* resultType, ::llvm::Value * source, const llvm::Twine &instName) {
622622
::llvm::LLVMContext& context = b.getContext();
623623
(void)context;
624624
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -651,7 +651,7 @@ index
651651
source
652652
};
653653

654-
return ::llvm::cast<FromFixedVectorOp>(b.CreateCall(fn, args, inst__name));
654+
return ::llvm::cast<FromFixedVectorOp>(b.CreateCall(fn, args, instName));
655655
}
656656

657657

@@ -788,7 +788,7 @@ source
788788

789789
const ::llvm::StringLiteral HandleGetOp::s_name{"xd.handle.get"};
790790

791-
HandleGetOp* HandleGetOp::create(llvm_dialects::Builder& b, const llvm::Twine &inst__name) {
791+
HandleGetOp* HandleGetOp::create(llvm_dialects::Builder& b, const llvm::Twine &instName) {
792792
::llvm::LLVMContext& context = b.getContext();
793793
(void)context;
794794
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -815,7 +815,7 @@ source
815815
assert(fn.getFunctionType() == fnType);
816816
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());
817817

818-
return ::llvm::cast<HandleGetOp>(b.CreateCall(fn, std::nullopt, inst__name));
818+
return ::llvm::cast<HandleGetOp>(b.CreateCall(fn, std::nullopt, instName));
819819
}
820820

821821

@@ -850,7 +850,7 @@ source
850850

851851
const ::llvm::StringLiteral IExtOp::s_name{"xd.iext"};
852852

853-
IExtOp* IExtOp::create(llvm_dialects::Builder& b, ::llvm::Type* resultType, ::llvm::Value * source, const llvm::Twine &inst__name) {
853+
IExtOp* IExtOp::create(llvm_dialects::Builder& b, ::llvm::Type* resultType, ::llvm::Value * source, const llvm::Twine &instName) {
854854
::llvm::LLVMContext& context = b.getContext();
855855
(void)context;
856856
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -883,7 +883,7 @@ source
883883
source
884884
};
885885

886-
return ::llvm::cast<IExtOp>(b.CreateCall(fn, args, inst__name));
886+
return ::llvm::cast<IExtOp>(b.CreateCall(fn, args, instName));
887887
}
888888

889889

@@ -948,7 +948,7 @@ source
948948

949949
const ::llvm::StringLiteral ITruncOp::s_name{"xd.itrunc"};
950950

951-
ITruncOp* ITruncOp::create(llvm_dialects::Builder& b, ::llvm::Type* resultType, ::llvm::Value * source, const llvm::Twine &inst__name) {
951+
ITruncOp* ITruncOp::create(llvm_dialects::Builder& b, ::llvm::Type* resultType, ::llvm::Value * source, const llvm::Twine &instName) {
952952
::llvm::LLVMContext& context = b.getContext();
953953
(void)context;
954954
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -981,7 +981,7 @@ source
981981
source
982982
};
983983

984-
return ::llvm::cast<ITruncOp>(b.CreateCall(fn, args, inst__name));
984+
return ::llvm::cast<ITruncOp>(b.CreateCall(fn, args, instName));
985985
}
986986

987987

@@ -1046,7 +1046,7 @@ source
10461046

10471047
const ::llvm::StringLiteral InsertElementOp::s_name{"xd.insertelement"};
10481048

1049-
InsertElementOp* InsertElementOp::create(llvm_dialects::Builder& b, ::llvm::Value * vector, ::llvm::Value * value, ::llvm::Value * index, const llvm::Twine &inst__name) {
1049+
InsertElementOp* InsertElementOp::create(llvm_dialects::Builder& b, ::llvm::Value * vector, ::llvm::Value * value, ::llvm::Value * index, const llvm::Twine &instName) {
10501050
::llvm::LLVMContext& context = b.getContext();
10511051
(void)context;
10521052
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1081,7 +1081,7 @@ value,
10811081
index
10821082
};
10831083

1084-
return ::llvm::cast<InsertElementOp>(b.CreateCall(fn, args, inst__name));
1084+
return ::llvm::cast<InsertElementOp>(b.CreateCall(fn, args, instName));
10851085
}
10861086

10871087

@@ -1168,7 +1168,7 @@ index
11681168

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

1171-
ReadOp* ReadOp::create(llvm_dialects::Builder& b, ::llvm::Type* dataType, const llvm::Twine &inst__name) {
1171+
ReadOp* ReadOp::create(llvm_dialects::Builder& b, ::llvm::Type* dataType, const llvm::Twine &instName) {
11721172
::llvm::LLVMContext& context = b.getContext();
11731173
(void)context;
11741174
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1198,7 +1198,7 @@ index
11981198
assert(fn.getFunctionType() == fnType);
11991199
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());
12001200

1201-
return ::llvm::cast<ReadOp>(b.CreateCall(fn, std::nullopt, inst__name));
1201+
return ::llvm::cast<ReadOp>(b.CreateCall(fn, std::nullopt, instName));
12021202
}
12031203

12041204

@@ -1225,7 +1225,7 @@ index
12251225

12261226
const ::llvm::StringLiteral SetReadOp::s_name{"xd.set.read"};
12271227

1228-
SetReadOp* SetReadOp::create(llvm_dialects::Builder& b, ::llvm::Type* dataType, const llvm::Twine &inst__name) {
1228+
SetReadOp* SetReadOp::create(llvm_dialects::Builder& b, ::llvm::Type* dataType, const llvm::Twine &instName) {
12291229
::llvm::LLVMContext& context = b.getContext();
12301230
(void)context;
12311231
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1255,7 +1255,7 @@ index
12551255
assert(fn.getFunctionType() == fnType);
12561256
assert(::llvm::cast<::llvm::Function>(fn.getCallee())->getFunctionType() == fn.getFunctionType());
12571257

1258-
return ::llvm::cast<SetReadOp>(b.CreateCall(fn, std::nullopt, inst__name));
1258+
return ::llvm::cast<SetReadOp>(b.CreateCall(fn, std::nullopt, instName));
12591259
}
12601260

12611261

@@ -1282,7 +1282,7 @@ index
12821282

12831283
const ::llvm::StringLiteral SetWriteOp::s_name{"xd.set.write"};
12841284

1285-
SetWriteOp* SetWriteOp::create(llvm_dialects::Builder& b, ::llvm::Value * data, const llvm::Twine &inst__name) {
1285+
SetWriteOp* SetWriteOp::create(llvm_dialects::Builder& b, ::llvm::Value * data, const llvm::Twine &instName) {
12861286
::llvm::LLVMContext& context = b.getContext();
12871287
(void)context;
12881288
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1312,7 +1312,7 @@ index
13121312
data
13131313
};
13141314

1315-
return ::llvm::cast<SetWriteOp>(b.CreateCall(fn, args, inst__name));
1315+
return ::llvm::cast<SetWriteOp>(b.CreateCall(fn, args, instName));
13161316
}
13171317

13181318

@@ -1345,7 +1345,7 @@ data
13451345

13461346
const ::llvm::StringLiteral SizeOfOp::s_name{"xd.sizeof"};
13471347

1348-
SizeOfOp* SizeOfOp::create(llvm_dialects::Builder& b, ::llvm::Type * sizeofType, const llvm::Twine &inst__name) {
1348+
SizeOfOp* SizeOfOp::create(llvm_dialects::Builder& b, ::llvm::Type * sizeofType, const llvm::Twine &instName) {
13491349
::llvm::LLVMContext& context = b.getContext();
13501350
(void)context;
13511351
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1375,7 +1375,7 @@ data
13751375
::llvm::PoisonValue::get(sizeofType)
13761376
};
13771377

1378-
return ::llvm::cast<SizeOfOp>(b.CreateCall(fn, args, inst__name));
1378+
return ::llvm::cast<SizeOfOp>(b.CreateCall(fn, args, instName));
13791379
}
13801380

13811381

@@ -1419,7 +1419,7 @@ data
14191419

14201420
const ::llvm::StringLiteral StreamAddOp::s_name{"xd.stream.add"};
14211421

1422-
StreamAddOp* StreamAddOp::create(llvm_dialects::Builder& b, ::llvm::Value * ptr, ::llvm::Value * count, ::llvm::Value * initial, const llvm::Twine &inst__name) {
1422+
StreamAddOp* StreamAddOp::create(llvm_dialects::Builder& b, ::llvm::Value * ptr, ::llvm::Value * count, ::llvm::Value * initial, const llvm::Twine &instName) {
14231423
::llvm::LLVMContext& context = b.getContext();
14241424
(void)context;
14251425
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1454,7 +1454,7 @@ count,
14541454
initial
14551455
};
14561456

1457-
return ::llvm::cast<StreamAddOp>(b.CreateCall(fn, args, inst__name));
1457+
return ::llvm::cast<StreamAddOp>(b.CreateCall(fn, args, instName));
14581458
}
14591459

14601460

@@ -1511,7 +1511,7 @@ initial
15111511

15121512
const ::llvm::StringLiteral StreamMaxOp::s_name{"xd.stream.max"};
15131513

1514-
StreamMaxOp* StreamMaxOp::create(llvm_dialects::Builder& b, ::llvm::Value * ptr, ::llvm::Value * count, ::llvm::Value * initial, const llvm::Twine &inst__name) {
1514+
StreamMaxOp* StreamMaxOp::create(llvm_dialects::Builder& b, ::llvm::Value * ptr, ::llvm::Value * count, ::llvm::Value * initial, const llvm::Twine &instName) {
15151515
::llvm::LLVMContext& context = b.getContext();
15161516
(void)context;
15171517
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1546,7 +1546,7 @@ count,
15461546
initial
15471547
};
15481548

1549-
return ::llvm::cast<StreamMaxOp>(b.CreateCall(fn, args, inst__name));
1549+
return ::llvm::cast<StreamMaxOp>(b.CreateCall(fn, args, instName));
15501550
}
15511551

15521552

@@ -1603,7 +1603,7 @@ initial
16031603

16041604
const ::llvm::StringLiteral StreamMinOp::s_name{"xd.stream.min"};
16051605

1606-
StreamMinOp* StreamMinOp::create(llvm_dialects::Builder& b, ::llvm::Value * ptr, ::llvm::Value * count, ::llvm::Value * initial, const llvm::Twine &inst__name) {
1606+
StreamMinOp* StreamMinOp::create(llvm_dialects::Builder& b, ::llvm::Value * ptr, ::llvm::Value * count, ::llvm::Value * initial, const llvm::Twine &instName) {
16071607
::llvm::LLVMContext& context = b.getContext();
16081608
(void)context;
16091609
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1638,7 +1638,7 @@ count,
16381638
initial
16391639
};
16401640

1641-
return ::llvm::cast<StreamMinOp>(b.CreateCall(fn, args, inst__name));
1641+
return ::llvm::cast<StreamMinOp>(b.CreateCall(fn, args, instName));
16421642
}
16431643

16441644

@@ -1695,7 +1695,7 @@ initial
16951695

16961696
const ::llvm::StringLiteral WriteOp::s_name{"xd.write"};
16971697

1698-
WriteOp* WriteOp::create(llvm_dialects::Builder& b, ::llvm::Value * data, const llvm::Twine &inst__name) {
1698+
WriteOp* WriteOp::create(llvm_dialects::Builder& b, ::llvm::Value * data, const llvm::Twine &instName) {
16991699
::llvm::LLVMContext& context = b.getContext();
17001700
(void)context;
17011701
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1725,7 +1725,7 @@ initial
17251725
data
17261726
};
17271727

1728-
return ::llvm::cast<WriteOp>(b.CreateCall(fn, args, inst__name));
1728+
return ::llvm::cast<WriteOp>(b.CreateCall(fn, args, instName));
17291729
}
17301730

17311731

@@ -1758,7 +1758,7 @@ data
17581758

17591759
const ::llvm::StringLiteral WriteVarArgOp::s_name{"xd.write.vararg"};
17601760

1761-
WriteVarArgOp* WriteVarArgOp::create(llvm_dialects::Builder& b, ::llvm::Value * data, ::llvm::ArrayRef<::llvm::Value *> args, const llvm::Twine &inst__name) {
1761+
WriteVarArgOp* WriteVarArgOp::create(llvm_dialects::Builder& b, ::llvm::Value * data, ::llvm::ArrayRef<::llvm::Value *> args, const llvm::Twine &instName) {
17621762
::llvm::LLVMContext& context = b.getContext();
17631763
(void)context;
17641764
::llvm::Module& module = *b.GetInsertBlock()->getModule();
@@ -1790,7 +1790,7 @@ data
17901790

17911791
arguments.append(args.begin(), args.end());
17921792

1793-
return ::llvm::cast<WriteVarArgOp>(b.CreateCall(fn, arguments, inst__name));
1793+
return ::llvm::cast<WriteVarArgOp>(b.CreateCall(fn, arguments, instName));
17941794
}
17951795

17961796

0 commit comments

Comments
 (0)