Skip to content

[mlir][tblgen] add concrete create methods #147168

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions mlir/include/mlir/IR/Builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,96 @@ class OpBuilder : public Builder {
Block::iterator insertPoint;
};

/// ImplicitLocOpBuilder maintains a 'current location', allowing use of the
/// create<> method without specifying the location. It is otherwise the same
/// as OpBuilder.
class ImplicitLocOpBuilder : public mlir::OpBuilder {
public:
/// OpBuilder has a bunch of convenience constructors - we support them all
/// with the additional Location.
template <typename... T>
ImplicitLocOpBuilder(Location loc, T &&...operands)
: OpBuilder(std::forward<T>(operands)...), curLoc(loc) {}

/// Create a builder and set the insertion point to before the first operation
/// in the block but still inside the block.
static ImplicitLocOpBuilder atBlockBegin(Location loc, Block *block,
Listener *listener = nullptr) {
return ImplicitLocOpBuilder(loc, block, block->begin(), listener);
}

/// Create a builder and set the insertion point to after the last operation
/// in the block but still inside the block.
static ImplicitLocOpBuilder atBlockEnd(Location loc, Block *block,
Listener *listener = nullptr) {
return ImplicitLocOpBuilder(loc, block, block->end(), listener);
}

/// Create a builder and set the insertion point to before the block
/// terminator.
static ImplicitLocOpBuilder atBlockTerminator(Location loc, Block *block,
Listener *listener = nullptr) {
auto *terminator = block->getTerminator();
assert(terminator != nullptr && "the block has no terminator");
return ImplicitLocOpBuilder(loc, block, Block::iterator(terminator),
listener);
}

/// Accessors for the implied location.
Location getLoc() const { return curLoc; }
void setLoc(Location loc) { curLoc = loc; }

// We allow clients to use the explicit-loc version of create as well.
using OpBuilder::create;
using OpBuilder::createOrFold;

/// Create an operation of specific op type at the current insertion point and
/// location.
template <typename OpTy, typename... Args>
OpTy create(Args &&...args) {
return OpBuilder::create<OpTy>(curLoc, std::forward<Args>(args)...);
}

/// Create an operation of specific op type at the current insertion point,
/// and immediately try to fold it. This functions populates 'results' with
/// the results after folding the operation.
template <typename OpTy, typename... Args>
void createOrFold(llvm::SmallVectorImpl<Value> &results, Args &&...args) {
OpBuilder::createOrFold<OpTy>(results, curLoc, std::forward<Args>(args)...);
}

/// Overload to create or fold a single result operation.
template <typename OpTy, typename... Args>
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::OneResult>(), Value>
createOrFold(Args &&...args) {
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
}

/// Overload to create or fold a zero result operation.
template <typename OpTy, typename... Args>
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::ZeroResults>(), OpTy>
createOrFold(Args &&...args) {
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
}

/// This builder can also be used to emit diagnostics to the current location.
mlir::InFlightDiagnostic
emitError(const llvm::Twine &message = llvm::Twine()) {
return mlir::emitError(curLoc, message);
}
mlir::InFlightDiagnostic
emitWarning(const llvm::Twine &message = llvm::Twine()) {
return mlir::emitWarning(curLoc, message);
}
mlir::InFlightDiagnostic
emitRemark(const llvm::Twine &message = llvm::Twine()) {
return mlir::emitRemark(curLoc, message);
}

private:
Location curLoc;
};

} // namespace mlir

#endif
94 changes: 0 additions & 94 deletions mlir/include/mlir/IR/ImplicitLocOpBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,98 +15,4 @@

#include "mlir/IR/Builders.h"

namespace mlir {

/// ImplicitLocOpBuilder maintains a 'current location', allowing use of the
/// create<> method without specifying the location. It is otherwise the same
/// as OpBuilder.
class ImplicitLocOpBuilder : public mlir::OpBuilder {
public:
/// OpBuilder has a bunch of convenience constructors - we support them all
/// with the additional Location.
template <typename... T>
ImplicitLocOpBuilder(Location loc, T &&...operands)
: OpBuilder(std::forward<T>(operands)...), curLoc(loc) {}

/// Create a builder and set the insertion point to before the first operation
/// in the block but still inside the block.
static ImplicitLocOpBuilder atBlockBegin(Location loc, Block *block,
Listener *listener = nullptr) {
return ImplicitLocOpBuilder(loc, block, block->begin(), listener);
}

/// Create a builder and set the insertion point to after the last operation
/// in the block but still inside the block.
static ImplicitLocOpBuilder atBlockEnd(Location loc, Block *block,
Listener *listener = nullptr) {
return ImplicitLocOpBuilder(loc, block, block->end(), listener);
}

/// Create a builder and set the insertion point to before the block
/// terminator.
static ImplicitLocOpBuilder atBlockTerminator(Location loc, Block *block,
Listener *listener = nullptr) {
auto *terminator = block->getTerminator();
assert(terminator != nullptr && "the block has no terminator");
return ImplicitLocOpBuilder(loc, block, Block::iterator(terminator),
listener);
}

/// Accessors for the implied location.
Location getLoc() const { return curLoc; }
void setLoc(Location loc) { curLoc = loc; }

// We allow clients to use the explicit-loc version of create as well.
using OpBuilder::create;
using OpBuilder::createOrFold;

/// Create an operation of specific op type at the current insertion point and
/// location.
template <typename OpTy, typename... Args>
OpTy create(Args &&...args) {
return OpBuilder::create<OpTy>(curLoc, std::forward<Args>(args)...);
}

/// Create an operation of specific op type at the current insertion point,
/// and immediately try to fold it. This functions populates 'results' with
/// the results after folding the operation.
template <typename OpTy, typename... Args>
void createOrFold(llvm::SmallVectorImpl<Value> &results, Args &&...args) {
OpBuilder::createOrFold<OpTy>(results, curLoc, std::forward<Args>(args)...);
}

/// Overload to create or fold a single result operation.
template <typename OpTy, typename... Args>
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::OneResult>(), Value>
createOrFold(Args &&...args) {
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
}

/// Overload to create or fold a zero result operation.
template <typename OpTy, typename... Args>
std::enable_if_t<OpTy::template hasTrait<mlir::OpTrait::ZeroResults>(), OpTy>
createOrFold(Args &&...args) {
return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
}

/// This builder can also be used to emit diagnostics to the current location.
mlir::InFlightDiagnostic
emitError(const llvm::Twine &message = llvm::Twine()) {
return mlir::emitError(curLoc, message);
}
mlir::InFlightDiagnostic
emitWarning(const llvm::Twine &message = llvm::Twine()) {
return mlir::emitWarning(curLoc, message);
}
mlir::InFlightDiagnostic
emitRemark(const llvm::Twine &message = llvm::Twine()) {
return mlir::emitRemark(curLoc, message);
}

private:
Location curLoc;
};

} // namespace mlir

#endif // MLIR_IR_IMPLICITLOCOPBUILDER_H
1 change: 1 addition & 0 deletions mlir/include/mlir/IR/OpDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace mlir {
class Builder;
class OpBuilder;
class ImplicitLocOpBuilder;

/// This class implements `Optional` functionality for ParseResult. We don't
/// directly use Optional here, because it provides an implicit conversion
Expand Down
4 changes: 4 additions & 0 deletions mlir/include/mlir/TableGen/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class MethodParameter {
StringRef getName() const { return name; }
/// Returns true if the parameter has a default value.
bool hasDefaultValue() const { return !defaultValue.empty(); }
/// Get the default value.
StringRef getDefaultValue() const { return defaultValue; }
/// Returns true if the parameter is optional.
bool isOptional() const { return optional; }

private:
/// The C++ type.
Expand Down
Loading