@@ -183,10 +183,10 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
183
183
llvm::StringMap<uint32_t > dynamicInitializerNames;
184
184
llvm::SmallVector<FuncOp, 4 > dynamicInitializers;
185
185
186
- // / List of ctors to be called before main()
187
- llvm::SmallVector<mlir::Attribute , 4 > globalCtorList;
188
- // / List of dtors to be called when unloading module.
189
- llvm::SmallVector<mlir::Attribute , 4 > globalDtorList;
186
+ // / List of ctors and their priorities to be called before main()
187
+ llvm::SmallVector<std::pair<std::string, uint32_t > , 4 > globalCtorList;
188
+ // / List of dtors and their priorities to be called when unloading module.
189
+ llvm::SmallVector<std::pair<std::string, uint32_t > , 4 > globalDtorList;
190
190
// / List of annotations in the module
191
191
llvm::SmallVector<mlir::Attribute, 4 > globalAnnotations;
192
192
};
@@ -939,14 +939,33 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
939
939
}
940
940
}
941
941
942
+ template <typename AttributeTy>
943
+ static llvm::SmallVector<mlir::Attribute>
944
+ prepareCtorDtorAttrList (mlir::MLIRContext *context,
945
+ llvm::ArrayRef<std::pair<std::string, uint32_t >> list) {
946
+ llvm::SmallVector<mlir::Attribute> attrs;
947
+ for (const auto &[name, priority] : list)
948
+ attrs.push_back (AttributeTy::get (context, name, priority));
949
+ return attrs;
950
+ }
951
+
942
952
void LoweringPreparePass::buildGlobalCtorDtorList () {
953
+
943
954
if (!globalCtorList.empty ()) {
955
+ llvm::SmallVector<mlir::Attribute> globalCtors =
956
+ prepareCtorDtorAttrList<cir::GlobalCtorAttr>(&getContext (),
957
+ globalCtorList);
958
+
944
959
theModule->setAttr (cir::CIRDialect::getGlobalCtorsAttrName (),
945
- mlir::ArrayAttr::get (&getContext (), globalCtorList ));
960
+ mlir::ArrayAttr::get (&getContext (), globalCtors ));
946
961
}
962
+
947
963
if (!globalDtorList.empty ()) {
964
+ llvm::SmallVector<mlir::Attribute> globalDtors =
965
+ prepareCtorDtorAttrList<cir::GlobalDtorAttr>(&getContext (),
966
+ globalDtorList);
948
967
theModule->setAttr (cir::CIRDialect::getGlobalDtorsAttrName (),
949
- mlir::ArrayAttr::get (&getContext (), globalDtorList ));
968
+ mlir::ArrayAttr::get (&getContext (), globalDtors ));
950
969
}
951
970
}
952
971
@@ -956,8 +975,9 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() {
956
975
957
976
for (auto &f : dynamicInitializers) {
958
977
// TODO: handle globals with a user-specified initialzation priority.
959
- auto ctorAttr = cir::GlobalCtorAttr::get (&getContext (), f.getName ());
960
- globalCtorList.push_back (ctorAttr);
978
+ // TODO: handle defaule priority more nicely.
979
+ globalCtorList.emplace_back (f.getName (),
980
+ cir::DefaultGlobalCtorDtorPriority);
961
981
}
962
982
963
983
SmallString<256 > fnName;
@@ -1118,7 +1138,9 @@ void LoweringPreparePass::buildCUDAModuleCtor() {
1118
1138
auto moduleCtor = buildRuntimeFunction (builder, moduleCtorName, loc,
1119
1139
FuncType::get ({}, voidTy),
1120
1140
GlobalLinkageKind::InternalLinkage);
1121
- globalCtorList.push_back (GlobalCtorAttr::get (&getContext (), moduleCtorName));
1141
+ // TODO figure out default mode priority
1142
+ globalCtorList.emplace_back (moduleCtorName,
1143
+ cir::DefaultGlobalCtorDtorPriority);
1122
1144
builder.setInsertionPointToStart (moduleCtor.addEntryBlock ());
1123
1145
1124
1146
// Register binary with CUDA runtime. This is substantially different in
@@ -1598,10 +1620,10 @@ void LoweringPreparePass::runOnOp(Operation *op) {
1598
1620
if (isa<cir::ArrayType>(valTy) || isa<cir::RecordType>(valTy))
1599
1621
lowerToMemCpy (storeOp);
1600
1622
} else if (auto fnOp = dyn_cast<cir::FuncOp>(op)) {
1601
- if (auto globalCtor = fnOp.getGlobalCtorAttr ()) {
1602
- globalCtorList.push_back ( globalCtor);
1603
- } else if (auto globalDtor = fnOp.getGlobalDtorAttr ()) {
1604
- globalDtorList.push_back ( globalDtor);
1623
+ if (auto globalCtor = fnOp.getGlobalCtorPriority ()) {
1624
+ globalCtorList.emplace_back (fnOp. getName (), globalCtor. value () );
1625
+ } else if (auto globalDtor = fnOp.getGlobalDtorPriority ()) {
1626
+ globalDtorList.emplace_back (fnOp. getName (), globalDtor. value () );
1605
1627
}
1606
1628
if (auto attr = fnOp.getExtraAttrs ().getElements ().get (
1607
1629
CUDAKernelNameAttr::getMnemonic ())) {
0 commit comments