Skip to content

Commit fb8215b

Browse files
authored
Merge branch 'llvm:main' into staticInit
2 parents 76ee1e8 + 368e7d9 commit fb8215b

30 files changed

+958
-155
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ def ConstStructAttr : CIR_Attr<"ConstStruct", "const_struct",
175175
];
176176

177177
let assemblyFormat = [{
178-
`<`
179-
custom<ConstStructMembers>($type, $members)
180-
`>`
178+
`<` custom<StructMembers>($members) `>`
181179
}];
182180

183181
let genVerifyDecl = 1;
@@ -307,20 +305,20 @@ def TypeInfoAttr : CIR_Attr<"TypeInfo", "typeinfo", [TypedAttrInterface]> {
307305
}];
308306

309307
let parameters = (ins AttributeSelfTypeParameter<"">:$type,
310-
"ConstStructAttr":$typeinfo_data);
308+
"mlir::ArrayAttr":$data);
311309

312310
let builders = [
313311
AttrBuilderWithInferredContext<(ins "Type":$type,
314-
"ConstStructAttr":$typeinfo_data), [{
315-
return $_get(type.getContext(), type, typeinfo_data);
312+
"mlir::ArrayAttr":$data), [{
313+
return $_get(type.getContext(), type, data);
316314
}]>
317315
];
318316

319317
// Checks struct element types should match the array for every equivalent
320318
// element type.
321319
let genVerifyDecl = 1;
322320
let assemblyFormat = [{
323-
`<` $typeinfo_data `>`
321+
`<` custom<StructMembers>($data) `>`
324322
}];
325323
}
326324

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,9 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods<RegionBranchO
12991299

13001300
return isDeclaration();
13011301
}
1302+
1303+
/// Whether the definition of this global may be replaced at link time.
1304+
bool isWeakForLinker() { return cir::isWeakForLinker(getLinkage()); }
13021305
}];
13031306

13041307
let skipDefaultBuilders = 1;

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
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

3134
namespace 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+
4146
public:
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

Comments
 (0)