@@ -106,8 +106,15 @@ getOrCreateGlobalKernelHandle(RewriterBase &rewriter, ModuleOp module,
106
106
// initialize the gloabl with global_ctors, as the initializer of global
107
107
// does not allow side effect
108
108
rewriter.setInsertionPointToStart (module .getBody ());
109
- LLVM::GlobalCtorsOp global_ctors =
110
- module .lookupSymbol <LLVM::GlobalCtorsOp>(getGlobalCtorsVarName ());
109
+ LLVM::GlobalCtorsOp global_ctors = nullptr ;
110
+ for (auto &op : module ->getRegion (0 ).front ()) {
111
+ auto ctorOp = dyn_cast<LLVM::GlobalCtorsOp>(op);
112
+ if (ctorOp) {
113
+ global_ctors = ctorOp;
114
+ break ;
115
+ }
116
+ }
117
+
111
118
SmallVector<Attribute> ctorRefs;
112
119
SmallVector<Attribute> priorities;
113
120
if (global_ctors) {
@@ -117,16 +124,20 @@ getOrCreateGlobalKernelHandle(RewriterBase &rewriter, ModuleOp module,
117
124
ctorRefs.push_back (ctor);
118
125
priorities.push_back (prior);
119
126
}
127
+ LLVM_DEBUG (llvm::dbgs ()
128
+ << " After append ctors: " << ctorRefs.size () << " \n " );
120
129
}
121
130
ctorRefs.push_back (ctorName);
122
131
// Set new ctor's priority to lowest
123
132
priorities.push_back (IntegerAttr::get (rewriter.getI32Type (), INT_MAX));
124
133
if (global_ctors) {
134
+ LLVM_DEBUG (llvm::dbgs () << " Replace existing ctors\n " );
125
135
// If there's existing ctors
126
136
rewriter.replaceOpWithNewOp <LLVM::GlobalCtorsOp>(
127
137
global_ctors, rewriter.getArrayAttr (ctorRefs),
128
138
rewriter.getArrayAttr (priorities));
129
139
} else {
140
+ LLVM_DEBUG (llvm::dbgs () << " Create new ctor\n " );
130
141
rewriter.create <LLVM::GlobalCtorsOp>(module .getLoc (),
131
142
rewriter.getArrayAttr (ctorRefs),
132
143
rewriter.getArrayAttr (priorities));
0 commit comments