2222#include " mlir/IR/Attributes.h"
2323#include " mlir/IR/Builders.h"
2424#include " mlir/IR/BuiltinAttributes.h"
25+ #include " mlir/IR/BuiltinOps.h"
2526#include " mlir/IR/BuiltinTypes.h"
2627#include " mlir/IR/Types.h"
2728#include " llvm/ADT/APSInt.h"
2829#include " llvm/ADT/FloatingPointMode.h"
30+ #include " llvm/ADT/StringMap.h"
2931#include " llvm/Support/ErrorHandling.h"
32+ #include < string>
3033
3134namespace cir {
3235
@@ -38,6 +41,8 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
3841 fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
3942 llvm::RoundingMode DefaultConstrainedRounding = llvm::RoundingMode::Dynamic;
4043
44+ llvm::StringMap<unsigned > GlobalsVersioning;
45+
4146public:
4247 CIRGenBuilderTy (mlir::MLIRContext &C, const CIRGenTypeCache &tc)
4348 : mlir::OpBuilder(&C), typeCache(tc) {}
@@ -144,7 +149,7 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
144149
145150 mlir::cir::TypeInfoAttr getTypeInfo (mlir::ArrayAttr fieldsAttr) {
146151 auto anonStruct = getAnonConstStruct (fieldsAttr);
147- return mlir::cir::TypeInfoAttr::get (anonStruct.getType (), anonStruct );
152+ return mlir::cir::TypeInfoAttr::get (anonStruct.getType (), fieldsAttr );
148153 }
149154
150155 mlir::TypedAttr getZeroInitAttr (mlir::Type ty) {
@@ -463,6 +468,36 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
463468 return Address (baseAddr, ptrTy, addr.getAlignment ());
464469 }
465470
471+ // FIXME(cir): CIRGenBuilder class should have an attribute with a reference
472+ // to the module so that we don't have search for it or pass it around.
473+ // FIXME(cir): Track a list of globals, or at least the last one inserted, so
474+ // that we can insert globals in the same order they are defined by CIRGen.
475+
476+ // / Creates a versioned global variable. If the symbol is already taken, an ID
477+ // / will be appended to the symbol. The returned global must always be queried
478+ // / for its name so it can be referenced correctly.
479+ [[nodiscard]] mlir::cir::GlobalOp
480+ createVersionedGlobal (mlir::ModuleOp module , mlir::Location loc,
481+ mlir::StringRef name, mlir::Type type, bool isConst,
482+ mlir::cir::GlobalLinkageKind linkage) {
483+ mlir::OpBuilder::InsertionGuard guard (*this );
484+ setInsertionPointToStart (module .getBody ());
485+
486+ // Create a unique name if the given name is already taken.
487+ std::string uniqueName;
488+ if (unsigned version = GlobalsVersioning[name.str ()]++)
489+ uniqueName = name.str () + " ." + std::to_string (version);
490+ else
491+ uniqueName = name.str ();
492+
493+ return create<mlir::cir::GlobalOp>(loc, uniqueName, type, isConst, linkage);
494+ }
495+
496+ mlir::Value createGetGlobal (mlir::cir::GlobalOp global) {
497+ return create<mlir::cir::GetGlobalOp>(
498+ global.getLoc (), getPointerTo (global.getSymType ()), global.getName ());
499+ }
500+
466501 // / Cast the element type of the given address to a different type,
467502 // / preserving information like the alignment.
468503 cir::Address createElementBitCast (mlir::Location loc, cir::Address addr,
0 commit comments