@@ -55,16 +55,26 @@ bool annotateSimdLoop(BasicBlock *incr)
5555 return false ;
5656}
5757
58- // / Pass that lowers a loop marked by annotateSimdLoop.
5958// / This pass should run after reduction variables have been converted to phi nodes,
6059// / otherwise floating-point reductions might not be recognized as such and
6160// / prevent SIMDization.
62- struct LowerSIMDLoop : public LoopPass {
61+ struct LowerSIMDLoop : public ModulePass {
6362 static char ID;
64- LowerSIMDLoop () : LoopPass(ID) {}
63+ LowerSIMDLoop () : ModulePass(ID)
64+ {
65+ }
66+
67+ protected:
68+ void getAnalysisUsage (AnalysisUsage &AU) const override
69+ {
70+ ModulePass::getAnalysisUsage (AU);
71+ AU.addRequired <LoopInfoWrapperPass>();
72+ AU.addPreserved <LoopInfoWrapperPass>();
73+ AU.setPreservesCFG ();
74+ }
6575
66- private:
67- bool runOnLoop (Loop *, LPPassManager &LPM ) override ;
76+ private:
77+ bool runOnModule (Module &M ) override ;
6878
6979 // / Check if loop has "simd_loop" annotation.
7080 // / If present, the annotation is an MDNode attached to an instruction in the loop's latch.
@@ -160,41 +170,65 @@ void LowerSIMDLoop::enableUnsafeAlgebraIfReduction(PHINode *Phi, Loop *L) const
160170 }
161171}
162172
163- bool LowerSIMDLoop::runOnLoop (Loop *L, LPPassManager &LPM )
173+ bool LowerSIMDLoop::runOnModule (Module &M )
164174{
165- if (!simd_loop_mdkind) {
166- simd_loop_mdkind = L->getHeader ()->getContext ().getMDKindID (" simd_loop" );
167- simd_loop_md = MDNode::get (L->getHeader ()->getContext (), ArrayRef<Metadata*>());
168- }
175+ Function *simdloop_marker = M.getFunction (" julia.simdloop_marker" );
169176
170- if (!hasSIMDLoopMetadata (L) )
177+ if (!simdloop_marker )
171178 return false ;
172179
173- DEBUG (dbgs () << " LSL: simd_loop found\n " );
174- BasicBlock *Lh = L->getHeader ();
175- DEBUG (dbgs () << " LSL: loop header: " << *Lh << " \n " );
176- MDNode *n = L->getLoopID ();
177- if (!n) {
178- // Loop does not have a LoopID yet, so give it one.
179- n = MDNode::get (Lh->getContext (), ArrayRef<Metadata*>(NULL ));
180- n->replaceOperandWith (0 ,n);
181- L->setLoopID (n);
182- }
183- MDNode *m = MDNode::get (Lh->getContext (), ArrayRef<Metadata*>(n));
180+ bool Changed = false ;
181+ std::vector<Instruction*> ToDelete;
182+ for (User *U : simdloop_marker->users ()) {
183+ Instruction *I = cast<Instruction>(U);
184+ ToDelete.push_back (I);
185+ LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(*I->getParent ()->getParent ()).getLoopInfo ();
186+ Loop *L = LI.getLoopFor (I->getParent ());
187+ I->removeFromParent ();
188+ if (!L)
189+ continue ;
190+
191+ DEBUG (dbgs () << " LSL: simd_loop found\n " );
192+ BasicBlock *Lh = L->getHeader ();
193+ DEBUG (dbgs () << " LSL: loop header: " << *Lh << " \n " );
194+ MDNode *n = L->getLoopID ();
195+ if (!n) {
196+ // Loop does not have a LoopID yet, so give it one.
197+ n = MDNode::get (Lh->getContext (), ArrayRef<Metadata *>(NULL ));
198+ n->replaceOperandWith (0 , n);
199+ L->setLoopID (n);
200+ }
201+
202+ assert (L->getLoopID ());
184203
185- // Mark memory references so that Loop::isAnnotatedParallel will return true for this loop.
186- for (Loop::block_iterator BBI = L->block_begin (), E=L->block_end (); BBI!=E; ++BBI)
187- for (BasicBlock::iterator I = (*BBI)->begin (), EE = (*BBI)->end (); I!=EE; ++I)
188- if (I->mayReadOrWriteMemory ())
189- I->setMetadata (" llvm.mem.parallel_loop_access" , m);
190- assert (L->isAnnotatedParallel ());
204+ MDNode *m = MDNode::get (Lh->getContext (), ArrayRef<Metadata *>(n));
205+
206+ // Mark memory references so that Loop::isAnnotatedParallel will return true for this loop.
207+ for (BasicBlock *BB : L->blocks ()) {
208+ for (Instruction &I : *BB) {
209+ if (I.mayReadOrWriteMemory ()) {
210+ I.setMetadata (LLVMContext::MD_mem_parallel_loop_access, m);
211+ }
212+ }
213+ }
214+ assert (L->isAnnotatedParallel ());
215+
216+ // Mark floating-point reductions as okay to reassociate/commute.
217+ for (BasicBlock::iterator I = Lh->begin (), E = Lh->end (); I != E; ++I) {
218+ if (PHINode *Phi = dyn_cast<PHINode>(I))
219+ enableUnsafeAlgebraIfReduction (Phi, L);
220+ else
221+ break ;
222+ }
223+
224+ Changed = true ;
225+ }
191226
192- // Mark floating-point reductions as okay to reassociate/commute.
193- for (BasicBlock::iterator I = Lh->begin (), E = Lh->end (); I!=E; ++I)
194- if (PHINode *Phi = dyn_cast<PHINode>(I))
195- enableUnsafeAlgebraIfReduction (Phi,L);
227+ for (Instruction *I : ToDelete)
228+ I->deleteValue ();
229+ simdloop_marker->eraseFromParent ();
196230
197- return true ;
231+ return Changed ;
198232}
199233
200234char LowerSIMDLoop::ID = 0 ;
0 commit comments