Skip to content

Commit bcfcabb

Browse files
committed
move ImplicitLocOpBuilder
1 parent 6534099 commit bcfcabb

File tree

4 files changed

+117
-105
lines changed

4 files changed

+117
-105
lines changed

mlir/include/mlir/IR/Builders.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,96 @@ class OpBuilder : public Builder {
615615
Block::iterator insertPoint;
616616
};
617617

618+
/// ImplicitLocOpBuilder maintains a 'current location', allowing use of the
619+
/// create<> method without specifying the location. It is otherwise the same
620+
/// as OpBuilder.
621+
class ImplicitLocOpBuilder : public mlir::OpBuilder {
622+
public:
623+
/// OpBuilder has a bunch of convenience constructors - we support them all
624+
/// with the additional Location.
625+
template <typename... T>
626+
ImplicitLocOpBuilder(Location loc, T &&...operands)
627+
: OpBuilder(std::forward<T>(operands)...), curLoc(loc) {}
628+
629+
/// Create a builder and set the insertion point to before the first operation
630+
/// in the block but still inside the block.
631+
static ImplicitLocOpBuilder atBlockBegin(Location loc, Block *block,
632+
Listener *listener = nullptr) {
633+
return ImplicitLocOpBuilder(loc, block, block->begin(), listener);
634+
}
635+
636+
/// Create a builder and set the insertion point to after the last operation
637+
/// in the block but still inside the block.
638+
static ImplicitLocOpBuilder atBlockEnd(Location loc, Block *block,
639+
Listener *listener = nullptr) {
640+
return ImplicitLocOpBuilder(loc, block, block->end(), listener);
641+
}
642+
643+
/// Create a builder and set the insertion point to before the block
644+
/// terminator.
645+
static ImplicitLocOpBuilder atBlockTerminator(Location loc, Block *block,
646+
Listener *listener = nullptr) {
647+
auto *terminator = block->getTerminator();
648+
assert(terminator != nullptr && "the block has no terminator");
649+
return ImplicitLocOpBuilder(loc, block, Block::iterator(terminator),
650+
listener);
651+
}
652+
653+
/// Accessors for the implied location.
654+
Location getLoc() const { return curLoc; }
655+
void setLoc(Location loc) { curLoc = loc; }
656+
657+
// We allow clients to use the explicit-loc version of create as well.
658+
using OpBuilder::create;
659+
using OpBuilder::createOrFold;
660+
661+
/// Create an operation of specific op type at the current insertion point and
662+
/// location.
663+
template <typename OpTy, typename... Args>
664+
OpTy create(Args &&...args) {
665+
return OpBuilder::create<OpTy>(curLoc, std::forward<Args>(args)...);
666+
}
667+
668+
/// Create an operation of specific op type at the current insertion point,
669+
/// and immediately try to fold it. This functions populates 'results' with
670+
/// the results after folding the operation.
671+
template <typename OpTy, typename... Args>
672+
void createOrFold(llvm::SmallVectorImpl<Value> &results, Args &&...args) {
673+
OpBuilder::createOrFold<OpTy>(results, curLoc, std::forward<Args>(args)...);
674+
}
675+
676+
/// Overload to create or fold a single result operation.
677+
template <typename OpTy, typename... Args>
678+
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::OneResult>(), Value>
679+
createOrFold(Args &&...args) {
680+
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
681+
}
682+
683+
/// Overload to create or fold a zero result operation.
684+
template <typename OpTy, typename... Args>
685+
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::ZeroResults>(), OpTy>
686+
createOrFold(Args &&...args) {
687+
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
688+
}
689+
690+
/// This builder can also be used to emit diagnostics to the current location.
691+
mlir::InFlightDiagnostic
692+
emitError(const llvm::Twine &message = llvm::Twine()) {
693+
return mlir::emitError(curLoc, message);
694+
}
695+
mlir::InFlightDiagnostic
696+
emitWarning(const llvm::Twine &message = llvm::Twine()) {
697+
return mlir::emitWarning(curLoc, message);
698+
}
699+
mlir::InFlightDiagnostic
700+
emitRemark(const llvm::Twine &message = llvm::Twine()) {
701+
return mlir::emitRemark(curLoc, message);
702+
}
703+
704+
private:
705+
Location curLoc;
706+
};
707+
618708
} // namespace mlir
619709

620710
#endif

mlir/include/mlir/IR/ImplicitLocOpBuilder.h

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -15,98 +15,4 @@
1515

1616
#include "mlir/IR/Builders.h"
1717

18-
namespace mlir {
19-
20-
/// ImplicitLocOpBuilder maintains a 'current location', allowing use of the
21-
/// create<> method without specifying the location. It is otherwise the same
22-
/// as OpBuilder.
23-
class ImplicitLocOpBuilder : public mlir::OpBuilder {
24-
public:
25-
/// OpBuilder has a bunch of convenience constructors - we support them all
26-
/// with the additional Location.
27-
template <typename... T>
28-
ImplicitLocOpBuilder(Location loc, T &&...operands)
29-
: OpBuilder(std::forward<T>(operands)...), curLoc(loc) {}
30-
31-
/// Create a builder and set the insertion point to before the first operation
32-
/// in the block but still inside the block.
33-
static ImplicitLocOpBuilder atBlockBegin(Location loc, Block *block,
34-
Listener *listener = nullptr) {
35-
return ImplicitLocOpBuilder(loc, block, block->begin(), listener);
36-
}
37-
38-
/// Create a builder and set the insertion point to after the last operation
39-
/// in the block but still inside the block.
40-
static ImplicitLocOpBuilder atBlockEnd(Location loc, Block *block,
41-
Listener *listener = nullptr) {
42-
return ImplicitLocOpBuilder(loc, block, block->end(), listener);
43-
}
44-
45-
/// Create a builder and set the insertion point to before the block
46-
/// terminator.
47-
static ImplicitLocOpBuilder atBlockTerminator(Location loc, Block *block,
48-
Listener *listener = nullptr) {
49-
auto *terminator = block->getTerminator();
50-
assert(terminator != nullptr && "the block has no terminator");
51-
return ImplicitLocOpBuilder(loc, block, Block::iterator(terminator),
52-
listener);
53-
}
54-
55-
/// Accessors for the implied location.
56-
Location getLoc() const { return curLoc; }
57-
void setLoc(Location loc) { curLoc = loc; }
58-
59-
// We allow clients to use the explicit-loc version of create as well.
60-
using OpBuilder::create;
61-
using OpBuilder::createOrFold;
62-
63-
/// Create an operation of specific op type at the current insertion point and
64-
/// location.
65-
template <typename OpTy, typename... Args>
66-
OpTy create(Args &&...args) {
67-
return OpBuilder::create<OpTy>(curLoc, std::forward<Args>(args)...);
68-
}
69-
70-
/// Create an operation of specific op type at the current insertion point,
71-
/// and immediately try to fold it. This functions populates 'results' with
72-
/// the results after folding the operation.
73-
template <typename OpTy, typename... Args>
74-
void createOrFold(llvm::SmallVectorImpl<Value> &results, Args &&...args) {
75-
OpBuilder::createOrFold<OpTy>(results, curLoc, std::forward<Args>(args)...);
76-
}
77-
78-
/// Overload to create or fold a single result operation.
79-
template <typename OpTy, typename... Args>
80-
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::OneResult>(), Value>
81-
createOrFold(Args &&...args) {
82-
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
83-
}
84-
85-
/// Overload to create or fold a zero result operation.
86-
template <typename OpTy, typename... Args>
87-
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::ZeroResults>(), OpTy>
88-
createOrFold(Args &&...args) {
89-
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
90-
}
91-
92-
/// This builder can also be used to emit diagnostics to the current location.
93-
mlir::InFlightDiagnostic
94-
emitError(const llvm::Twine &message = llvm::Twine()) {
95-
return mlir::emitError(curLoc, message);
96-
}
97-
mlir::InFlightDiagnostic
98-
emitWarning(const llvm::Twine &message = llvm::Twine()) {
99-
return mlir::emitWarning(curLoc, message);
100-
}
101-
mlir::InFlightDiagnostic
102-
emitRemark(const llvm::Twine &message = llvm::Twine()) {
103-
return mlir::emitRemark(curLoc, message);
104-
}
105-
106-
private:
107-
Location curLoc;
108-
};
109-
110-
} // namespace mlir
111-
11218
#endif // MLIR_IR_IMPLICITLOCOPBUILDER_H

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
namespace mlir {
3131
class Builder;
3232
class OpBuilder;
33+
class ImplicitLocOpBuilder;
3334

3435
/// This class implements `Optional` functionality for ParseResult. We don't
3536
/// directly use Optional here, because it provides an implicit conversion

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ static const char *const inlineCreateBody = R"(
238238
return __res__;
239239
)";
240240

241+
static const char *const inlineCreateBodyImplicitLoc = R"(
242+
return create(builder, builder.getLoc(){0});
243+
)";
244+
241245
//===----------------------------------------------------------------------===//
242246
// Utility structs and functions
243247
//===----------------------------------------------------------------------===//
@@ -2579,35 +2583,47 @@ static bool canInferType(const Operator &op) {
25792583

25802584
void OpEmitter::genInlineCreateBody(
25812585
const SmallVector<MethodParameter> &paramList) {
2582-
SmallVector<MethodParameter> createParamList;
2586+
SmallVector<MethodParameter> createParamListOpBuilder;
2587+
SmallVector<MethodParameter> createParamListImplicitLocOpBuilder;
25832588
SmallVector<llvm::StringRef, 4> nonBuilderStateArgsList;
2584-
createParamList.emplace_back("::mlir::OpBuilder &", "builder");
2589+
createParamListOpBuilder.emplace_back("::mlir::OpBuilder &", "builder");
2590+
createParamListImplicitLocOpBuilder.emplace_back(
2591+
"::mlir::ImplicitLocOpBuilder &", "builder");
25852592
std::string locParamName = "location";
25862593
while (llvm::find_if(paramList, [&locParamName](const MethodParameter &p) {
25872594
return p.getName() == locParamName;
25882595
}) != paramList.end()) {
25892596
locParamName += "_";
25902597
}
2591-
createParamList.emplace_back("::mlir::Location", locParamName);
2598+
createParamListOpBuilder.emplace_back("::mlir::Location", locParamName);
25922599

25932600
for (auto &param : paramList) {
25942601
if (param.getType() == "::mlir::OpBuilder &" ||
25952602
param.getType() == "::mlir::OperationState &")
25962603
continue;
2597-
createParamList.emplace_back(param.getType(), param.getName(),
2598-
param.getDefaultValue(), param.isOptional());
2604+
createParamListOpBuilder.emplace_back(param.getType(), param.getName(),
2605+
param.getDefaultValue(),
2606+
param.isOptional());
2607+
createParamListImplicitLocOpBuilder.emplace_back(
2608+
param.getType(), param.getName(), param.getDefaultValue(),
2609+
param.isOptional());
25992610
nonBuilderStateArgsList.push_back(param.getName());
26002611
}
2601-
auto *c = opClass.addStaticMethod(opClass.getClassName(), "create",
2602-
createParamList);
2612+
auto *cWithLoc = opClass.addStaticMethod(opClass.getClassName(), "create",
2613+
createParamListOpBuilder);
2614+
auto *cImplicitLoc = opClass.addStaticMethod(
2615+
opClass.getClassName(), "create", createParamListImplicitLocOpBuilder);
26032616
std::string nonBuilderStateArgs = "";
26042617
if (!nonBuilderStateArgsList.empty()) {
26052618
llvm::raw_string_ostream nonBuilderStateArgsOS(nonBuilderStateArgs);
26062619
interleaveComma(nonBuilderStateArgsList, nonBuilderStateArgsOS);
26072620
nonBuilderStateArgs = ", " + nonBuilderStateArgs;
26082621
}
2609-
c->body() << llvm::formatv(inlineCreateBody, locParamName,
2610-
nonBuilderStateArgs, opClass.getClassName());
2622+
cWithLoc->body() << llvm::formatv(inlineCreateBody, locParamName,
2623+
nonBuilderStateArgs,
2624+
opClass.getClassName());
2625+
cImplicitLoc->body() << llvm::formatv(inlineCreateBodyImplicitLoc,
2626+
nonBuilderStateArgs);
26112627
}
26122628

26132629
void OpEmitter::genSeparateArgParamBuilder() {
@@ -3087,8 +3103,7 @@ void OpEmitter::genBuilder() {
30873103

30883104
std::optional<StringRef> body = builder.getBody();
30893105
auto properties = body ? Method::Static : Method::StaticDeclaration;
3090-
auto *method =
3091-
opClass.addMethod("void", "build", properties, std::move(arguments));
3106+
auto *method = opClass.addMethod("void", "build", properties, arguments);
30923107
if (body)
30933108
ERROR_IF_PRUNED(method, "build", op);
30943109

0 commit comments

Comments
 (0)