Skip to content

Commit b6f943e

Browse files
mgehre-amdSimon Camphausen
andcommitted
EmitC: Add emitc.global and emitc.get_global (#145) (llvm#88701)
This adds - `emitc.global` and `emitc.get_global` ops to model global variables similar to how `memref.global` and `memref.get_global` work. - translation of those ops to C++ - lowering of `memref.global` and `memref.get_global` into those ops --------- Co-authored-by: Simon Camphausen <[email protected]>
1 parent f33095e commit b6f943e

File tree

7 files changed

+31
-21
lines changed

7 files changed

+31
-21
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
10201020
The `emitc.global` operation declares or defines a named global variable.
10211021
The backing memory for the variable is allocated statically and is
10221022
described by the type of the variable.
1023-
Optionally, and `initial_value` can be provided.
1023+
Optionally, an `initial_value` can be provided.
10241024
Internal linkage can be specified using the `staticSpecifier` unit attribute
10251025
and external linkage can be specified using the `externSpecifier` unit attribute.
10261026
Note that the default linkage without those two keywords depends on whether
@@ -1047,14 +1047,14 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
10471047
let arguments = (ins SymbolNameAttr:$sym_name,
10481048
TypeAttr:$type,
10491049
OptionalAttr<EmitC_OpaqueOrTypedAttr>:$initial_value,
1050-
UnitAttr:$externSpecifier,
1051-
UnitAttr:$staticSpecifier,
1052-
UnitAttr:$constSpecifier);
1050+
UnitAttr:$extern_specifier,
1051+
UnitAttr:$static_specifier,
1052+
UnitAttr:$const_specifier);
10531053

10541054
let assemblyFormat = [{
1055-
(`extern` $externSpecifier^)?
1056-
(`static` $staticSpecifier^)?
1057-
(`const` $constSpecifier^)?
1055+
(`extern` $extern_specifier^)?
1056+
(`static` $static_specifier^)?
1057+
(`const` $const_specifier^)?
10581058
$sym_name
10591059
`:` custom<EmitCGlobalOpTypeAndInitialValue>($type, $initial_value)
10601060
attr-dict

mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> {
8181
op.getLoc(),
8282
"only public and private visibility is currently supported");
8383
}
84-
// We are explicit in specifier the linkage because the default linkage
84+
// We are explicit in specifing the linkage because the default linkage
8585
// for constants is different in C and C++.
8686
bool staticSpecifier = visibility == SymbolTable::Visibility::Private;
8787
bool externSpecifier = !staticSpecifier;
8888

89+
Attribute initialValue = operands.getInitialValueAttr();
90+
if (isa_and_present<UnitAttr>(initialValue))
91+
initialValue = {};
92+
8993
rewriter.replaceOpWithNewOp<emitc::GlobalOp>(
90-
op, operands.getSymName(), resultTy, operands.getInitialValueAttr(),
91-
externSpecifier, staticSpecifier, operands.getConstant());
94+
op, operands.getSymName(), resultTy, initialValue, externSpecifier,
95+
staticSpecifier, operands.getConstant());
9296
return success();
9397
}
9498
};

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,16 +1015,18 @@ parseEmitCGlobalOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr,
10151015
if (parser.parseAttribute(initialValue, getInitializerTypeForGlobal(type)))
10161016
return failure();
10171017

1018-
if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr>(initialValue))
1018+
if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(
1019+
initialValue))
10191020
return parser.emitError(parser.getNameLoc())
1020-
<< "initial value should be a unit, integer, float or elements "
1021+
<< "initial value should be a integer, float, elements or opaque "
10211022
"attribute";
10221023
return success();
10231024
}
10241025

10251026
LogicalResult GlobalOp::verify() {
1026-
// Verify that the initial value, if present, is either a unit attribute or
1027-
// an elements attribute.
1027+
if (!isSupportedEmitCType(getType())) {
1028+
return emitOpError("expected valid emitc type");
1029+
}
10281030
if (getInitialValue().has_value()) {
10291031
Attribute initValue = getInitialValue().value();
10301032
// Check that the type of the initial value is compatible with the type of
@@ -1050,10 +1052,9 @@ LogicalResult GlobalOp::verify() {
10501052
return emitOpError("initial value expected to be of type ")
10511053
<< getType() << ", but was of type " << floatAttr.getType();
10521054
}
1053-
} else {
1054-
return emitOpError(
1055-
"initial value should be a unit, integer, float or elements "
1056-
"attribute, but got ")
1055+
} else if (!isa<emitc::OpaqueAttr>(initValue)) {
1056+
return emitOpError("initial value should be a integer, float, elements "
1057+
"or opaque attribute, but got ")
10571058
<< initValue;
10581059
}
10591060
}

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ LogicalResult CppEmitter::emitGlobalVariable(GlobalOp op) {
14171417
}
14181418

14191419
std::optional<Attribute> initialValue = op.getInitialValue();
1420-
if (initialValue && !isa<UnitAttr>(*initialValue)) {
1420+
if (initialValue) {
14211421
os << " = ";
14221422
if (failed(emitAttribute(op->getLoc(), *initialValue)))
14231423
return failure();

mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ module @globals {
3636
// CHECK: emitc.global static const @internal_global : !emitc.array<3x7xf32> = dense<4.000000e+00>
3737
memref.global @public_global : memref<3x7xf32>
3838
// CHECK: emitc.global extern @public_global : !emitc.array<3x7xf32>
39+
memref.global @uninitialized_global : memref<3x7xf32> = uninitialized
40+
// CHECK: emitc.global extern @uninitialized_global : !emitc.array<3x7xf32>
3941

4042
func.func @use_global() {
4143
// CHECK: emitc.get_global @public_global : !emitc.array<3x7xf32>

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,6 @@ emitc.global const @myconstant : !emitc.array<2xi16> = dense<2>
242242

243243
func.func @use_global(%i: index) -> f32 {
244244
%0 = emitc.get_global @myglobal : !emitc.array<2xf32>
245-
%1 = emitc.subscript %0[%i] : <2xf32>, index
245+
%1 = emitc.subscript %0[%i] : (!emitc.array<2xf32>, index) -> f32
246246
return %1 : f32
247247
}

mlir/test/Target/Cpp/global.mlir

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ emitc.global static @static_var : f32
2525
emitc.global static @static_const : f32 = 3.0
2626
// CHECK: static float static_const = 3.000000000e+00f;
2727

28+
emitc.global @opaque_init : !emitc.opaque<"char"> = #emitc.opaque<"CHAR_MIN">
29+
// CHECK: char opaque_init = CHAR_MIN;
30+
2831
func.func @use_global(%i: index) -> f32 {
2932
%0 = emitc.get_global @myglobal : !emitc.array<2xf32>
30-
%1 = emitc.subscript %0[%i] : <2xf32>, index
33+
%1 = emitc.subscript %0[%i] : (!emitc.array<2xf32>, index) -> f32
3134
return %1 : f32
3235
// CHECK-LABEL: use_global
3336
// CHECK-SAME: (size_t [[V1:.*]])

0 commit comments

Comments
 (0)