Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -1023,8 +1023,10 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
The backing memory for the variable is allocated statically and is
described by the type of the variable.
Optionally, and `initial_value` can be provided.
Internal linkage can be specified using the `internal` unit attribute
and external linkage can be specified using the `external` unit attribute.
Internal linkage can be specified using the `staticSpecifier` unit attribute
and external linkage can be specified using the `externSpecifier` unit attribute.
Note that the default linkage without those two keywords depends on whether
the target is C or C++ and whether the global variable is `const`.
The global variable can also be marked constant using the
`const` unit attribute. Writing to such constant global variables is
undefined.
Expand All @@ -1047,18 +1049,16 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
let arguments = (ins SymbolNameAttr:$sym_name,
TypeAttr:$type,
OptionalAttr<EmitC_OpaqueOrTypedAttr>:$initial_value,
UnitAttr:$external,
UnitAttr:$internal,
UnitAttr:$externSpecifier,
UnitAttr:$staticSpecifier,
UnitAttr:$constant);

let assemblyFormat = [{
(`extern` $external^)? (`static` $internal^)? (`const` $constant^)?
(`extern` $externSpecifier^)? (`static` $staticSpecifier^)? (`const` $constant^)?
$sym_name `:` custom<EmitCGlobalOpTypeAndInitialValue>($type, $initial_value) attr-dict
}];

let extraClassDeclaration = [{
bool hasExternalLinkage() { return getExternal(); }
bool hasInternalLinkage() { return getInternal(); }
bool isConstant() { return getConstant(); }
}];
let hasVerifier = 1;
Expand Down
7 changes: 5 additions & 2 deletions mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,15 @@ struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> {
"cannot convert result type");
}

bool staticLinkage =
// We are explicit in specifier the linkage because the default linkage
// for constants is different in C and C++.
bool staticSpecifier =
SymbolTable::getSymbolVisibility(op) != SymbolTable::Visibility::Public;
bool externSpecifier = !staticSpecifier;

rewriter.replaceOpWithNewOp<emitc::GlobalOp>(
op, operands.getSymName(), resultTy, operands.getInitialValueAttr(),
/*external=*/false, staticLinkage, operands.getConstant());
externSpecifier, staticSpecifier, operands.getConstant());
return success();
}
};
Expand Down
6 changes: 2 additions & 4 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,9 @@ LogicalResult GlobalOp::verify() {
"attribute, but got ")
<< initValue;
}
} else if (isConstant() && !hasExternalLinkage()) {
return emitOpError("cannot define uninitialized constant");
}
if (hasExternalLinkage() && hasInternalLinkage()) {
return emitOpError("cannot have internal and external linkage");
if (getStaticSpecifier() && getExternSpecifier()) {
return emitOpError("cannot have both static and extern specifiers");
}
return success();
}
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,9 +1403,9 @@ LogicalResult CppEmitter::emitVariableDeclaration(OpResult result,
}

LogicalResult CppEmitter::emitGlobalVariable(GlobalOp op) {
if (op.hasExternalLinkage())
if (op.getExternSpecifier())
os << "extern ";
else if (op.hasInternalLinkage())
else if (op.getStaticSpecifier())
os << "static ";
if (op.getConstant())
os << "const ";
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module @globals {
memref.global "private" constant @internal_global : memref<3x7xf32> = dense<4.0>
// CHECK: emitc.global static const @internal_global : !emitc.array<3x7xf32> = dense<4.000000e+00>
memref.global @public_global : memref<3x7xf32>
// CHECK: emitc.global @public_global : !emitc.array<3x7xf32>
// CHECK: emitc.global extern @public_global : !emitc.array<3x7xf32>

func.func @use_global() {
// CHECK: emitc.get_global @public_global : !emitc.array<3x7xf32>
Expand Down
7 changes: 1 addition & 6 deletions mlir/test/Dialect/EmitC/invalid_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,7 @@ func.func @logical_or_resulterror(%arg0: i32, %arg1: i32) {

// -----

// expected-error @+1 {{'emitc.global' op cannot define uninitialized constant}}
emitc.global const @uninit : i32

// -----

// expected-error @+1 {{'emitc.global' op cannot have internal and external linkage}}
// expected-error @+1 {{'emitc.global' op cannot have both static and extern specifiers}}
emitc.global extern static @uninit : i32

// -----
Expand Down