// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
- let arguments = (ins SymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
- let arguments = (ins SymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
- let arguments = (ins SymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
- let arguments = (ins SymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
- let arguments = (ins SymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs);
// The generic call operation returns a single value of TensorType.
let results = (outs F64Tensor);
private:
/// Return a symbol reference to the printf function, inserting it into the
/// module if necessary.
- static SymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter,
- ModuleOp module,
- LLVM::LLVMDialect *llvmDialect) {
+ static FlatSymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter,
+ ModuleOp module,
+ LLVM::LLVMDialect *llvmDialect) {
auto *context = module.getContext();
if (module.lookupSymbol<LLVM::LLVMFuncOp>("printf"))
return SymbolRefAttr::get("printf", context);
// The generic call operation takes a symbol reference attribute as the
// callee, and inputs for the call.
- let arguments = (ins SymbolRefAttr:$callee, Variadic<Toy_Type>:$inputs);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<Toy_Type>:$inputs);
// The generic call operation returns a single value of TensorType or
// StructType.
private:
/// Return a symbol reference to the printf function, inserting it into the
/// module if necessary.
- static SymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter,
- ModuleOp module,
- LLVM::LLVMDialect *llvmDialect) {
+ static FlatSymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter,
+ ModuleOp module,
+ LLVM::LLVMDialect *llvmDialect) {
auto *context = module.getContext();
if (module.lookupSymbol<LLVM::LLVMFuncOp>("printf"))
return SymbolRefAttr::get("printf", context);
Syntax:
``` {.ebnf}
-symbol-ref-attribute ::= symbol-ref-id
+symbol-ref-attribute ::= symbol-ref-id (`::` symbol-ref-id)*
```
A symbol reference attribute is a literal attribute that represents a named
reference to an operation that is nested within an operation with the
`OpTrait::SymbolTable` trait. As such, this reference is given meaning by the
-nearest parent operation containing the `OpTrait::SymbolTable` trait.
+nearest parent operation containing the `OpTrait::SymbolTable` trait. It may
+optionally contain a set of nested references that further resolve to a symbol
+nested within a different symbol table.
This attribute can only be held internally by
[array attributes](#array-attribute) and
```c++
/// Return a symbol reference to the printf function, inserting it into the
/// module if necessary.
-static SymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter,
- ModuleOp module,
- LLVM::LLVMDialect *llvmDialect) {
+static FlatSymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter,
+ ModuleOp module,
+ LLVM::LLVMDialect *llvmDialect) {
auto *context = module.getContext();
if (module.lookupSymbol<LLVM::LLVMFuncOp>("printf"))
return SymbolRefAttr::get("printf", context);
// Call-related operations.
def LLVM_CallOp : LLVM_Op<"call">,
- Arguments<(ins OptionalAttr<SymbolRefAttr>:$callee,
+ Arguments<(ins OptionalAttr<FlatSymbolRefAttr>:$callee,
Variadic<LLVM_Type>)>,
Results<(outs Variadic<LLVM_Type>)>,
LLVM_TwoBuilders<LLVM_OneResultOpBuilder,
// to work correctly).
def LLVM_AddressOfOp
: LLVM_OneResultOp<"mlir.addressof">,
- Arguments<(ins SymbolRefAttr:$global_name)> {
+ Arguments<(ins FlatSymbolRefAttr:$global_name)> {
let builders = [
OpBuilder<"Builder *builder, OperationState &result, LLVMType resType, "
"StringRef name, ArrayRef<NamedAttribute> attrs = {}", [{
I64ArrayAttr:$n_loop_types,
I64ArrayAttr:$n_views,
OptionalAttr<StrAttr>:$doc,
- OptionalAttr<SymbolRefAttr>:$fun,
+ OptionalAttr<FlatSymbolRefAttr>:$fun,
OptionalAttr<StrAttr>:$library_call);
let regions = (region AnyRegion:$region);
let extraClassDeclaration = [{
Where #trait_attributes is an alias of a dictionary attribute containing:
- doc [optional]: a documentation string
- - fun: a SymbolRefAttr that must resolve to an existing function symbol.
+ - fun: a FlatSymbolRefAttr that must resolve to an existing function symbol.
To support inplace updates in a generic fashion, the signature of the
function must be:
```
Where #trait_attributes is an alias of a dictionary attribute containing:
- doc [optional]: a documentation string
- - fun: a SymbolRefAttr that must resolve to an existing function symbol.
+ - fun: a FlatSymbolRefAttr that must resolve to an existing function symbol.
To support inplace updates in a generic fashion, the signature of the
function must be:
```
}];
let arguments = (ins
- SymbolRefAttr:$callee,
+ FlatSymbolRefAttr:$callee,
Variadic<SPV_Type>:$arguments
);
}];
let arguments = (ins
- SymbolRefAttr:$fn,
+ FlatSymbolRefAttr:$fn,
SPV_ExecutionModeAttr:$execution_mode,
I32ArrayAttr:$values
);
}];
let arguments = (ins
- SymbolRefAttr:$variable
+ FlatSymbolRefAttr:$variable
);
let results = (outs
let arguments = (ins
SPV_ExecutionModelAttr:$execution_model,
- SymbolRefAttr:$fn,
+ FlatSymbolRefAttr:$fn,
SymbolRefArrayAttr:$interface
);
let arguments = (ins
TypeAttr:$type,
StrAttr:$sym_name,
- OptionalAttr<SymbolRefAttr>:$initializer
+ OptionalAttr<FlatSymbolRefAttr>:$initializer
);
let builders = [
}];
let arguments = (ins
- SymbolRefAttr:$spec_const
+ FlatSymbolRefAttr:$spec_const
);
let results = (outs
def CallOp : Std_Op<"call", [CallOpInterface]> {
let summary = "call operation";
let description = [{
- The "call" operation represents a direct call to a function. The operands
- and result types of the call must match the specified function type. The
- callee is encoded as a function attribute named "callee".
+ The "call" operation represents a direct call to a function that is within
+ the same symbol scope as the call. The operands and result types of the
+ call must match the specified function type. The callee is encoded as a
+ function attribute named "callee".
%2 = call @my_add(%0, %1) : (f32, f32) -> f32
}];
- let arguments = (ins SymbolRefAttr:$callee, Variadic<AnyType>:$operands);
+ let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$operands);
let results = (outs Variadic<AnyType>);
let builders = [OpBuilder<
struct FloatAttributeStorage;
struct OpaqueAttributeStorage;
struct StringAttributeStorage;
+struct SymbolRefAttributeStorage;
struct TypeAttributeStorage;
/// Elements Attributes.
};
} // namespace StandardAttributes
+//===----------------------------------------------------------------------===//
+// AffineMapAttr
+//===----------------------------------------------------------------------===//
+
class AffineMapAttr
: public Attribute::AttrBase<AffineMapAttr, Attribute,
detail::AffineMapAttributeStorage> {
}
};
+//===----------------------------------------------------------------------===//
+// ArrayAttr
+//===----------------------------------------------------------------------===//
+
/// Array attributes are lists of other attributes. They are not necessarily
/// type homogenous given that attributes don't, in general, carry types.
class ArrayAttr : public Attribute::AttrBase<ArrayAttr, Attribute,
}
};
+//===----------------------------------------------------------------------===//
+// BoolAttr
+//===----------------------------------------------------------------------===//
+
class BoolAttr : public Attribute::AttrBase<BoolAttr, Attribute,
detail::BoolAttributeStorage> {
public:
static bool kindof(unsigned kind) { return kind == StandardAttributes::Bool; }
};
+//===----------------------------------------------------------------------===//
+// DictionaryAttr
+//===----------------------------------------------------------------------===//
+
/// NamedAttribute is used for dictionary attributes, it holds an identifier for
/// the name and a value for the attribute. The attribute pointer should always
/// be non-null.
}
};
+//===----------------------------------------------------------------------===//
+// FloatAttr
+//===----------------------------------------------------------------------===//
+
class FloatAttr : public Attribute::AttrBase<FloatAttr, Attribute,
detail::FloatAttributeStorage> {
public:
Type type, const APFloat &value);
};
+//===----------------------------------------------------------------------===//
+// IntegerAttr
+//===----------------------------------------------------------------------===//
+
class IntegerAttr
: public Attribute::AttrBase<IntegerAttr, Attribute,
detail::IntegerAttributeStorage> {
}
};
+//===----------------------------------------------------------------------===//
+// IntegerSetAttr
+//===----------------------------------------------------------------------===//
+
class IntegerSetAttr
: public Attribute::AttrBase<IntegerSetAttr, Attribute,
detail::IntegerSetAttributeStorage> {
}
};
+//===----------------------------------------------------------------------===//
+// OpaqueAttr
+//===----------------------------------------------------------------------===//
+
/// Opaque attributes represent attributes of non-registered dialects. These are
/// attribute represented in their raw string form, and can only usefully be
/// tested for attribute equality.
}
};
+//===----------------------------------------------------------------------===//
+// StringAttr
+//===----------------------------------------------------------------------===//
+
class StringAttr : public Attribute::AttrBase<StringAttr, Attribute,
detail::StringAttributeStorage> {
public:
}
};
+//===----------------------------------------------------------------------===//
+// SymbolRefAttr
+//===----------------------------------------------------------------------===//
+
+class FlatSymbolRefAttr;
+
/// A symbol reference attribute represents a symbolic reference to another
/// operation.
class SymbolRefAttr
: public Attribute::AttrBase<SymbolRefAttr, Attribute,
- detail::StringAttributeStorage> {
+ detail::SymbolRefAttributeStorage> {
public:
using Base::Base;
- using ValueType = StringRef;
- static SymbolRefAttr get(StringRef value, MLIRContext *ctx);
+ /// Construct a symbol reference for the given value name.
+ static FlatSymbolRefAttr get(StringRef value, MLIRContext *ctx);
- /// Returns the name of the held symbol reference.
- StringRef getValue() const;
+ /// Construct a symbol reference for the given value name, and a set of nested
+ /// references that are further resolve to a nested symbol.
+ static SymbolRefAttr get(StringRef value,
+ ArrayRef<FlatSymbolRefAttr> references,
+ MLIRContext *ctx);
+
+ /// Returns the name of the top level symbol reference, i.e. the root of the
+ /// reference path.
+ StringRef getRootReference() const;
+
+ /// Returns the name of the fully resolved symbol, i.e. the leaf of the
+ /// reference path.
+ StringRef getLeafReference() const;
+
+ /// Returns the set of nested references representing the path to the symbol
+ /// nested under the root reference.
+ ArrayRef<FlatSymbolRefAttr> getNestedReferences() const;
/// Methods for support type inquiry through isa, cast, and dyn_cast.
static bool kindof(unsigned kind) {
}
};
+/// A symbol reference with a reference path containing a single element. This
+/// is used to refer to an operation within the current symbol table.
+class FlatSymbolRefAttr : public SymbolRefAttr {
+public:
+ using SymbolRefAttr::SymbolRefAttr;
+ using ValueType = StringRef;
+
+ /// Construct a symbol reference for the given value name.
+ static FlatSymbolRefAttr get(StringRef value, MLIRContext *ctx) {
+ return SymbolRefAttr::get(value, ctx);
+ }
+
+ /// Returns the name of the held symbol reference.
+ StringRef getValue() const { return getRootReference(); }
+
+ /// Methods for support type inquiry through isa, cast, and dyn_cast.
+ static bool classof(Attribute attr) {
+ SymbolRefAttr refAttr = attr.dyn_cast<SymbolRefAttr>();
+ return refAttr && refAttr.getNestedReferences().empty();
+ }
+
+private:
+ using SymbolRefAttr::get;
+ using SymbolRefAttr::getNestedReferences;
+};
+
+//===----------------------------------------------------------------------===//
+// Type
+//===----------------------------------------------------------------------===//
+
class TypeAttr : public Attribute::AttrBase<TypeAttr, Attribute,
detail::TypeAttributeStorage> {
public:
static bool kindof(unsigned kind) { return kind == StandardAttributes::Type; }
};
+//===----------------------------------------------------------------------===//
+// UnitAttr
+//===----------------------------------------------------------------------===//
+
/// Unit attributes are attributes that hold no specific value and are given
/// meaning by their existence.
class UnitAttr : public Attribute::AttrBase<UnitAttr> {
FloatAttr getFloatAttr(Type type, const APFloat &value);
StringAttr getStringAttr(StringRef bytes);
ArrayAttr getArrayAttr(ArrayRef<Attribute> value);
- SymbolRefAttr getSymbolRefAttr(Operation *value);
- SymbolRefAttr getSymbolRefAttr(StringRef value);
+ FlatSymbolRefAttr getSymbolRefAttr(Operation *value);
+ FlatSymbolRefAttr getSymbolRefAttr(StringRef value);
+ SymbolRefAttr getSymbolRefAttr(StringRef value,
+ ArrayRef<FlatSymbolRefAttr> nestedReferences);
// Returns a 0-valued attribute of the given `type`. This function only
// supports boolean, integer, and 16-/32-/64-bit float types, and vector or
/// to. This may return null in the case of an external callable object, e.g.
/// an external function.
Region *getCallableRegion(CallInterfaceCallable callable) {
- assert(callable.get<SymbolRefAttr>().getValue() == getName());
+ assert(callable.get<SymbolRefAttr>().getLeafReference() == getName());
return isExternal() ? nullptr : &getBody();
}
def SymbolRefAttr : Attr<CPred<"$_self.isa<SymbolRefAttr>()">,
"symbol reference attribute"> {
let storageType = [{ SymbolRefAttr }];
+ let returnType = [{ SymbolRefAttr }];
+ let constBuilderCall = "$_builder.getSymbolRefAttr($0)";
+ let convertFromStorage = "$_self";
+}
+def FlatSymbolRefAttr : Attr<CPred<"$_self.isa<FlatSymbolRefAttr>()">,
+ "flat symbol reference attribute"> {
+ let storageType = [{ FlatSymbolRefAttr }];
let returnType = [{ StringRef }];
let constBuilderCall = "$_builder.getSymbolRefAttr($0)";
+ let convertFromStorage = "$_self.getValue()";
}
def SymbolRefArrayAttr :
def IsNullAttr : AttrConstraint<
CPred<"!$_self">, "empty attribute (for optional attributes)">;
-// An attribute constraint on SymbolRefAttr that requires the SymbolRefAttr
-// pointing to an op of `opClass` within the closest parent with a symbol table.
+// An attribute constraint on FlatSymbolRefAttr that requires that the
+// reference point to an op of `opClass` within the closest parent with a symbol
+// table.
+// TODO(riverriddle) Add support for nested symbol references.
class ReferToOp<string opClass> : AttrConstraint<
CPred<"isa_and_nonnull<" # opClass # ">("
"::mlir::SymbolTable::lookupNearestSymbolFrom("
- "&$_op, $_self.cast<SymbolRefAttr>().getValue()))">,
+ "&$_op, $_self.cast<FlatSymbolRefAttr>().getValue()))">,
"referencing to a '" # opClass # "' symbol">;
//===----------------------------------------------------------------------===//
// Get the callee operation from the callable.
Operation *callee;
if (auto symbolRef = callable.dyn_cast<SymbolRefAttr>())
- callee = SymbolTable::lookupNearestSymbolFrom(from, symbolRef.getValue());
+ // TODO(riverriddle) Support nested references.
+ callee = SymbolTable::lookupNearestSymbolFrom(from,
+ symbolRef.getRootReference());
else
callee = callable.get<Value *>()->getDefiningOp();
}
StringRef LaunchFuncOp::getKernelModuleName() {
- return getAttrOfType<SymbolRefAttr>(getKernelModuleAttrName()).getValue();
+ return getAttrOfType<SymbolRefAttr>(getKernelModuleAttrName())
+ .getRootReference();
}
Value *LaunchFuncOp::getKernelOperand(unsigned i) {
if (Optional<SymbolTable::UseRange> symbolUses =
SymbolTable::getSymbolUses(symbolDefWorklist.pop_back_val())) {
for (SymbolTable::SymbolUse symbolUse : *symbolUses) {
- StringRef symbolName = symbolUse.getSymbolRef().getValue();
+ StringRef symbolName =
+ symbolUse.getSymbolRef().cast<FlatSymbolRefAttr>().getValue();
if (moduleManager.lookupSymbol(symbolName))
continue;
// Get a SymbolRefAttr containing the library function name for the LinalgOp.
// If the library function does not exist, insert a declaration.
template <typename LinalgOp>
-static SymbolRefAttr getLibraryCallSymbolRef(Operation *op,
- PatternRewriter &rewriter) {
+static FlatSymbolRefAttr getLibraryCallSymbolRef(Operation *op,
+ PatternRewriter &rewriter) {
auto linalgOp = cast<LinalgOp>(op);
auto fnName = linalgOp.getLibraryCallName();
if (fnName.empty()) {
}
// fnName is a dynamic std::String, unique it via a SymbolRefAttr.
- SymbolRefAttr fnNameAttr = rewriter.getSymbolRefAttr(fnName);
+ FlatSymbolRefAttr fnNameAttr = rewriter.getSymbolRefAttr(fnName);
auto module = op->getParentOfType<ModuleOp>();
if (module.lookupSymbol(fnName)) {
return fnNameAttr;
static ParseResult parseAddressOfOp(OpAsmParser &parser,
OperationState &state) {
- SymbolRefAttr varRefAttr;
+ FlatSymbolRefAttr varRefAttr;
Type type;
if (parser.parseAttribute(varRefAttr, Type(), kVariableAttrName,
state.attributes) ||
SmallVector<Type, 0> idTypes;
SmallVector<Attribute, 4> interfaceVars;
- SymbolRefAttr fn;
+ FlatSymbolRefAttr fn;
if (parseEnumAttribute(execModel, parser, state) ||
parser.parseAttribute(fn, Type(), kFnNameAttrName, state.attributes)) {
return failure();
do {
// The name of the interface variable attribute isnt important
auto attrName = "var_symbol";
- SymbolRefAttr var;
+ FlatSymbolRefAttr var;
SmallVector<NamedAttribute, 1> attrs;
if (parser.parseAttribute(var, Type(), attrName, attrs)) {
return failure();
static ParseResult parseFunctionCallOp(OpAsmParser &parser,
OperationState &state) {
- SymbolRefAttr calleeAttr;
+ FlatSymbolRefAttr calleeAttr;
FunctionType type;
SmallVector<OpAsmParser::OperandType, 4> operands;
auto loc = parser.getNameLoc();
// Parse optional initializer
if (succeeded(parser.parseOptionalKeyword(kInitializerAttrName))) {
- SymbolRefAttr initSymbol;
+ FlatSymbolRefAttr initSymbol;
if (parser.parseLParen() ||
parser.parseAttribute(initSymbol, Type(), kInitializerAttrName,
state.attributes) ||
if (varOp.storageClass() == spirv::StorageClass::Generic)
return varOp.emitOpError("storage class cannot be 'Generic'");
- if (auto init = varOp.getAttrOfType<SymbolRefAttr>(kInitializerAttrName)) {
+ if (auto init =
+ varOp.getAttrOfType<FlatSymbolRefAttr>(kInitializerAttrName)) {
auto moduleOp = varOp.getParentOfType<spirv::ModuleOp>();
auto *initOp = moduleOp.lookupSymbol(init.getValue());
// TODO: Currently only variable initialization with specialization
}
if (auto interface = entryPointOp.interface()) {
for (Attribute varRef : interface) {
- auto varSymRef = varRef.dyn_cast<SymbolRefAttr>();
+ auto varSymRef = varRef.dyn_cast<FlatSymbolRefAttr>();
if (!varSymRef) {
return entryPointOp.emitError(
"expected symbol reference for interface "
static ParseResult parseReferenceOfOp(OpAsmParser &parser,
OperationState &state) {
- SymbolRefAttr constRefAttr;
+ FlatSymbolRefAttr constRefAttr;
Type type;
if (parser.parseAttribute(constRefAttr, Type(), kSpecConstAttrName,
state.attributes) ||
wordIndex++;
// Initializer.
- SymbolRefAttr initializer = nullptr;
+ FlatSymbolRefAttr initializer = nullptr;
if (wordIndex < operands.size()) {
auto initializerOp = getGlobalVariable(operands[wordIndex]);
if (!initializerOp) {
// Add the interface values.
if (auto interface = op.interface()) {
for (auto var : interface.getValue()) {
- auto id = getVariableID(var.cast<SymbolRefAttr>().getValue());
+ auto id = getVariableID(var.cast<FlatSymbolRefAttr>().getValue());
if (!id) {
return op.emitError("referencing undefined global variable."
"spv.EntryPoint is at the end of spv.module. All "
//===----------------------------------------------------------------------===//
static ParseResult parseCallOp(OpAsmParser &parser, OperationState &result) {
- SymbolRefAttr calleeAttr;
+ FlatSymbolRefAttr calleeAttr;
FunctionType calleeType;
SmallVector<OpAsmParser::OperandType, 4> operands;
auto calleeLoc = parser.getNameLoc();
static LogicalResult verify(CallOp op) {
// Check that the callee attribute was specified.
- auto fnAttr = op.getAttrOfType<SymbolRefAttr>("callee");
+ auto fnAttr = op.getAttrOfType<FlatSymbolRefAttr>("callee");
if (!fnAttr)
return op.emitOpError("requires a 'callee' symbol reference attribute");
auto fn =
// Replace with a direct call.
SmallVector<Type, 8> callResults(indirectCall.getResultTypes());
SmallVector<Value *, 8> callOperands(indirectCall.getArgOperands());
- rewriter.replaceOpWithNewOp<CallOp>(indirectCall, calledFn.getValue(),
- callResults, callOperands);
+ rewriter.replaceOpWithNewOp<CallOp>(indirectCall, calledFn, callResults,
+ callOperands);
return matchSuccess();
}
};
}
if (type.isa<FunctionType>()) {
- auto fnAttr = value.dyn_cast<SymbolRefAttr>();
+ auto fnAttr = value.dyn_cast<FlatSymbolRefAttr>();
if (!fnAttr)
return op.emitOpError("requires 'value' to be a function reference");
case StandardAttributes::Type:
printType(attr.cast<TypeAttr>().getValue());
break;
- case StandardAttributes::SymbolRef:
- printSymbolReference(attr.cast<SymbolRefAttr>().getValue(), os);
+ case StandardAttributes::SymbolRef: {
+ auto refAttr = attr.dyn_cast<SymbolRefAttr>();
+ printSymbolReference(refAttr.getRootReference(), os);
+ for (FlatSymbolRefAttr nestedRef : refAttr.getNestedReferences()) {
+ os << "::";
+ printSymbolReference(nestedRef.getValue(), os);
+ }
break;
+ }
case StandardAttributes::OpaqueElements: {
auto eltsAttr = attr.cast<OpaqueElementsAttr>();
os << "opaque<\"" << eltsAttr.getDialect()->getNamespace() << "\", ";
StringRef value;
};
+/// An attribute representing a symbol reference.
+struct SymbolRefAttributeStorage final
+ : public AttributeStorage,
+ public llvm::TrailingObjects<SymbolRefAttributeStorage,
+ FlatSymbolRefAttr> {
+ using KeyTy = std::pair<StringRef, ArrayRef<FlatSymbolRefAttr>>;
+
+ SymbolRefAttributeStorage(StringRef value, size_t numNestedRefs)
+ : value(value), numNestedRefs(numNestedRefs) {}
+
+ /// Key equality function.
+ bool operator==(const KeyTy &key) const {
+ return key == KeyTy(value, getNestedRefs());
+ }
+
+ /// Construct a new storage instance.
+ static SymbolRefAttributeStorage *
+ construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
+ auto size = SymbolRefAttributeStorage::totalSizeToAlloc<FlatSymbolRefAttr>(
+ key.second.size());
+ auto rawMem = allocator.allocate(size, alignof(SymbolRefAttributeStorage));
+ auto result = ::new (rawMem) SymbolRefAttributeStorage(
+ allocator.copyInto(key.first), key.second.size());
+ std::uninitialized_copy(key.second.begin(), key.second.end(),
+ result->getTrailingObjects<FlatSymbolRefAttr>());
+ return result;
+ }
+
+ /// Returns the set of nested references.
+ ArrayRef<FlatSymbolRefAttr> getNestedRefs() const {
+ return {getTrailingObjects<FlatSymbolRefAttr>(), numNestedRefs};
+ }
+
+ StringRef value;
+ size_t numNestedRefs;
+};
+
/// An attribute representing a reference to a type.
struct TypeAttributeStorage : public AttributeStorage {
using KeyTy = Type;
// SymbolRefAttr
//===----------------------------------------------------------------------===//
-SymbolRefAttr SymbolRefAttr::get(StringRef value, MLIRContext *ctx) {
- return Base::get(ctx, StandardAttributes::SymbolRef, value,
- NoneType::get(ctx));
+FlatSymbolRefAttr SymbolRefAttr::get(StringRef value, MLIRContext *ctx) {
+ return Base::get(ctx, StandardAttributes::SymbolRef, value, llvm::None)
+ .cast<FlatSymbolRefAttr>();
}
-StringRef SymbolRefAttr::getValue() const { return getImpl()->value; }
+SymbolRefAttr SymbolRefAttr::get(StringRef value,
+ ArrayRef<FlatSymbolRefAttr> nestedReferences,
+ MLIRContext *ctx) {
+ return Base::get(ctx, StandardAttributes::SymbolRef, value, nestedReferences);
+}
+
+StringRef SymbolRefAttr::getRootReference() const { return getImpl()->value; }
+
+StringRef SymbolRefAttr::getLeafReference() const {
+ ArrayRef<FlatSymbolRefAttr> nestedRefs = getNestedReferences();
+ return nestedRefs.empty() ? getRootReference() : nestedRefs.back().getValue();
+}
+
+ArrayRef<FlatSymbolRefAttr> SymbolRefAttr::getNestedReferences() const {
+ return getImpl()->getNestedRefs();
+}
//===----------------------------------------------------------------------===//
// IntegerAttr
return ArrayAttr::get(value, context);
}
-SymbolRefAttr Builder::getSymbolRefAttr(Operation *value) {
+FlatSymbolRefAttr Builder::getSymbolRefAttr(Operation *value) {
auto symName =
value->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName());
assert(symName && "value does not have a valid symbol name");
return getSymbolRefAttr(symName.getValue());
}
-SymbolRefAttr Builder::getSymbolRefAttr(StringRef value) {
+FlatSymbolRefAttr Builder::getSymbolRefAttr(StringRef value) {
return SymbolRefAttr::get(value, getContext());
}
+SymbolRefAttr
+Builder::getSymbolRefAttr(StringRef value,
+ ArrayRef<FlatSymbolRefAttr> nestedReferences) {
+ return SymbolRefAttr::get(value, nestedReferences, getContext());
+}
ArrayAttr Builder::getI32ArrayAttr(ArrayRef<int32_t> values) {
auto attrs = functional::map(
auto &builder = parser.getBuilder();
// Parse the name as a symbol reference attribute.
- SymbolRefAttr nameAttr;
+ FlatSymbolRefAttr nameAttr;
if (parser.parseAttribute(nameAttr, ::mlir::SymbolTable::getSymbolAttrName(),
result.attributes))
return failure();
/// | type
/// | `[` (attribute-value (`,` attribute-value)*)? `]`
/// | `{` (attribute-entry (`,` attribute-entry)*)? `}`
-/// | symbol-ref-id
+/// | symbol-ref-id (`::` symbol-ref-id)*
/// | `dense` `<` attribute-value `>` `:`
/// (tensor-type | vector-type)
/// | `sparse` `<` attribute-value `,` attribute-value `>`
case Token::at_identifier: {
std::string nameStr = extractSymbolReference(getToken());
consumeToken(Token::at_identifier);
- return builder.getSymbolRefAttr(nameStr);
+
+ // Parse any nested references.
+ std::vector<FlatSymbolRefAttr> nestedRefs;
+ while (getToken().is(Token::colon)) {
+ // Check for the '::' prefix.
+ const char *curPointer = getToken().getLoc().getPointer();
+ consumeToken(Token::colon);
+ if (!consumeIf(Token::colon)) {
+ state.lex.resetPointer(curPointer);
+ consumeToken();
+ break;
+ }
+ // Parse the reference itself.
+ auto curLoc = getToken().getLoc();
+ if (getToken().isNot(Token::at_identifier)) {
+ emitError(curLoc, "expected nested symbol reference identifier");
+ return Attribute();
+ }
+
+ std::string nameStr = extractSymbolReference(getToken());
+ consumeToken(Token::at_identifier);
+ nestedRefs.push_back(SymbolRefAttr::get(nameStr, getContext()));
+ }
+
+ return builder.getSymbolRefAttr(nameStr, nestedRefs);
}
// Parse a 'unit' attribute.
return llvm::ConstantInt::get(llvmType, intAttr.getValue());
if (auto floatAttr = attr.dyn_cast<FloatAttr>())
return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
- if (auto funcAttr = attr.dyn_cast<SymbolRefAttr>())
+ if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>())
return functionMapping.lookup(funcAttr.getValue());
if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) {
auto *sequentialType = cast<llvm::SequentialType>(llvmType);
auto convertCall = [this, &builder](Operation &op) -> llvm::Value * {
auto operands = lookupValues(op.getOperands());
ArrayRef<llvm::Value *> operandsRef(operands);
- if (auto attr = op.getAttrOfType<SymbolRefAttr>("callee")) {
+ if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) {
return builder.CreateCall(functionMapping.lookup(attr.getValue()),
operandsRef);
} else {
"foo.symbol_reference"() {ref = @"\"_string_symbol_reference\""} : () -> ()
return
}
+
+// CHECK-LABEL: func @nested_reference
+// CHECK-NEXT: ref = @some_symbol::@some_nested_symbol
+func @nested_reference() attributes {test.ref = @some_symbol::@some_nested_symbol }
def SymbolRefOp : TEST_Op<"symbol_ref_attr"> {
let arguments = (ins
- Confined<SymbolRefAttr, [ReferToOp<"FuncOp">]>:$symbol
+ Confined<FlatSymbolRefAttr, [ReferToOp<"FuncOp">]>:$symbol
);
}
def ConversionCallOp : TEST_Op<"conversion_call_op",
[CallOpInterface]> {
- let arguments = (ins Variadic<AnyType>:$inputs, SymbolRefAttr:$callee);
+ let arguments = (ins Variadic<AnyType>:$inputs, FlatSymbolRefAttr:$callee);
let results = (outs Variadic<AnyType>);
let extraClassDeclaration = [{
/// Return the callee of this operation.
CallInterfaceCallable getCallableForCallee() {
- return getAttrOfType<SymbolRefAttr>("callee");
+ return getAttrOfType<FlatSymbolRefAttr>("callee");
}
}];
}
// CHECK: auto tblgen_cAttr = this->getAttr("cAttr");
// CHECK-NEXT: if (tblgen_cAttr) {
// CHECK-NEXT: if (!((some-condition))) return emitOpError("attribute 'cAttr' failed to satisfy constraint: some attribute kind");
-
+
def SomeTypeAttr : TypeAttrBase<"SomeType", "some type attribute">;
def BOp : NS_Op<"b_op", []> {
F64Attr:$f64_attr,
StrAttr:$str_attr,
ElementsAttr:$elements_attr,
- SymbolRefAttr:$function_attr,
+ FlatSymbolRefAttr:$function_attr,
SomeTypeAttr:$type_attr,
ArrayAttr:$array_attr,
TypedArrayAttrBase<SomeAttr, "SomeAttr array">:$some_attr_array,
// CHECK: if (!(((tblgen_f64_attr.isa<FloatAttr>())) && ((tblgen_f64_attr.cast<FloatAttr>().getType().isF64()))))
// CHECK: if (!((tblgen_str_attr.isa<StringAttr>())))
// CHECK: if (!((tblgen_elements_attr.isa<ElementsAttr>())))
-// CHECK: if (!((tblgen_function_attr.isa<SymbolRefAttr>())))
+// CHECK: if (!((tblgen_function_attr.isa<FlatSymbolRefAttr>())))
// CHECK: if (!(((tblgen_type_attr.isa<TypeAttr>())) && ((tblgen_type_attr.cast<TypeAttr>().getValue().isa<SomeType>()))))
// CHECK: if (!((tblgen_array_attr.isa<ArrayAttr>())))
// CHECK: if (!(((tblgen_some_attr_array.isa<ArrayAttr>())) && (llvm::all_of(tblgen_some_attr_array.cast<ArrayAttr>(), [](Attribute attr) { return (some-condition); }))))