static StringRef getOperationName() { return "module"; }
- static void build(Builder *builder, OperationState &result);
+ static void build(Builder *builder, OperationState &result,
+ StringRef name = {});
- /// Construct a module from the given location.
- static ModuleOp create(Location loc);
+ /// Construct a module from the given location with an optional name.
+ static ModuleOp create(Location loc, StringRef name = {});
/// Operation hooks.
static ParseResult parse(OpAsmParser &parser, OperationState &result);
Region &getBodyRegion();
Block *getBody();
+ /// Return the name of this module if present.
+ StringRef getName();
+
/// Print the this module in the custom top-level form.
void print(raw_ostream &os);
void dump();
// Module Operation.
//===----------------------------------------------------------------------===//
-void ModuleOp::build(Builder *builder, OperationState &result) {
+void ModuleOp::build(Builder *builder, OperationState &result, StringRef name) {
ensureTerminator(*result.addRegion(), *builder, result.location);
+ if (!name.empty())
+ result.attributes.push_back(
+ builder->getNamedAttr(mlir::SymbolTable::getSymbolAttrName(),
+ builder->getSymbolRefAttr(name)));
}
/// Construct a module from the given context.
-ModuleOp ModuleOp::create(Location loc) {
+ModuleOp ModuleOp::create(Location loc, StringRef name) {
OperationState state(loc, "module");
Builder builder(loc->getContext());
- ModuleOp::build(&builder, state);
+ ModuleOp::build(&builder, state, name);
return llvm::cast<ModuleOp>(Operation::create(state));
}
ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) {
+ // If the name is present, parse it.
+ StringAttr nameAttr;
+ (void)parser.parseSymbolName(nameAttr, mlir::SymbolTable::getSymbolAttrName(),
+ result.attributes);
+
// If module attributes are present, parse them.
if (succeeded(parser.parseOptionalKeyword("attributes")))
if (parser.parseOptionalAttributeDict(result.attributes))
void ModuleOp::print(OpAsmPrinter &p) {
p << "module";
+ StringRef name = getName();
+ if (!name.empty())
+ p << " @" << name;
+
// Print the module attributes.
auto attrs = getAttrs();
- if (!attrs.empty()) {
+ if (!attrs.empty() &&
+ !(attrs.size() == 1 && attrs.front().first.strref() ==
+ mlir::SymbolTable::getSymbolAttrName())) {
p << " attributes";
- p.printOptionalAttrDict(attrs, {});
+ p.printOptionalAttrDict(attrs, {mlir::SymbolTable::getSymbolAttrName()});
}
// Print the region.
if (body->getNumArguments() != 0)
return emitOpError("expected body to have no arguments");
- // Check that none of the attributes are non-dialect attributes.
+ // Check that none of the attributes are non-dialect attributes, except for
+ // the symbol name attribute.
for (auto attr : getOperation()->getAttrList().getAttrs()) {
- if (!attr.first.strref().contains('.'))
+ if (!attr.first.strref().contains('.') &&
+ attr.first.strref() != mlir::SymbolTable::getSymbolAttrName())
return emitOpError(
"can only contain dialect-specific attributes, found: '")
<< attr.first << "'";
/// Return body of this module.
Region &ModuleOp::getBodyRegion() { return getOperation()->getRegion(0); }
Block *ModuleOp::getBody() { return &getBodyRegion().front(); }
+
+StringRef ModuleOp::getName() {
+ if (auto nameAttr =
+ getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName()))
+ return nameAttr.getValue();
+ return {};
+}