@@ -291,6 +291,86 @@ ValueCategory MLIRScanner::VisitForStmt(clang::ForStmt *fors) {
291
291
return nullptr ;
292
292
}
293
293
294
+ ValueCategory MLIRScanner::VisitCXXForRangeStmt (clang::CXXForRangeStmt *fors) {
295
+ IfScope scope (*this );
296
+
297
+ auto loc = getMLIRLocation (fors->getForLoc ());
298
+
299
+ if (auto *s = fors->getInit ()) {
300
+ Visit (s);
301
+ }
302
+ Visit (fors->getRangeStmt ());
303
+ Visit (fors->getBeginStmt ());
304
+ Visit (fors->getEndStmt ());
305
+
306
+ auto i1Ty = builder.getIntegerType (1 );
307
+ auto type = mlir::MemRefType::get ({}, i1Ty, {}, 0 );
308
+ auto truev = builder.create <ConstantIntOp>(loc, true , 1 );
309
+
310
+ LoopContext lctx{builder.create <mlir::memref::AllocaOp>(loc, type),
311
+ builder.create <mlir::memref::AllocaOp>(loc, type)};
312
+ builder.create <mlir::memref::StoreOp>(loc, truev, lctx.noBreak );
313
+
314
+ auto *toadd = builder.getInsertionBlock ()->getParent ();
315
+ auto &condB = *(new Block ());
316
+ toadd->getBlocks ().push_back (&condB);
317
+ auto &bodyB = *(new Block ());
318
+ toadd->getBlocks ().push_back (&bodyB);
319
+ auto &exitB = *(new Block ());
320
+ toadd->getBlocks ().push_back (&exitB);
321
+
322
+ builder.create <mlir::cf::BranchOp>(loc, &condB);
323
+
324
+ builder.setInsertionPointToStart (&condB);
325
+
326
+ if (auto *s = fors->getCond ()) {
327
+ auto condRes = Visit (s);
328
+ auto cond = condRes.getValue (builder);
329
+ if (auto LT = cond.getType ().dyn_cast <mlir::LLVM::LLVMPointerType>()) {
330
+ auto nullptr_llvm = builder.create <mlir::LLVM::NullOp>(loc, LT);
331
+ cond = builder.create <mlir::LLVM::ICmpOp>(
332
+ loc, mlir::LLVM::ICmpPredicate::ne, cond, nullptr_llvm);
333
+ }
334
+ auto ty = cond.getType ().cast <mlir::IntegerType>();
335
+ if (ty.getWidth () != 1 ) {
336
+ cond = builder.create <arith::CmpIOp>(
337
+ loc, CmpIPredicate::ne, cond,
338
+ builder.create <ConstantIntOp>(loc, 0 , ty));
339
+ }
340
+ auto nb = builder.create <mlir::memref::LoadOp>(loc, lctx.noBreak ,
341
+ std::vector<mlir::Value>());
342
+ cond = builder.create <AndIOp>(loc, cond, nb);
343
+ builder.create <mlir::cf::CondBranchOp>(loc, cond, &bodyB, &exitB);
344
+ } else {
345
+ auto cond = builder.create <mlir::memref::LoadOp>(
346
+ loc, lctx.noBreak , std::vector<mlir::Value>());
347
+ builder.create <mlir::cf::CondBranchOp>(loc, cond, &bodyB, &exitB);
348
+ }
349
+
350
+ builder.setInsertionPointToStart (&bodyB);
351
+ builder.create <mlir::memref::StoreOp>(
352
+ loc,
353
+ builder.create <mlir::memref::LoadOp>(loc, lctx.noBreak ,
354
+ std::vector<mlir::Value>()),
355
+ lctx.keepRunning , std::vector<mlir::Value>());
356
+
357
+ loops.push_back (lctx);
358
+ Visit (fors->getLoopVarStmt ());
359
+ Visit (fors->getBody ());
360
+ if (auto *s = fors->getInc ()) {
361
+ IfScope scope (*this );
362
+ Visit (s);
363
+ }
364
+ loops.pop_back ();
365
+ if (builder.getInsertionBlock ()->empty () ||
366
+ !isTerminator (&builder.getInsertionBlock ()->back ())) {
367
+ builder.create <mlir::cf::BranchOp>(loc, &condB);
368
+ }
369
+
370
+ builder.setInsertionPointToStart (&exitB);
371
+ return nullptr ;
372
+ }
373
+
294
374
ValueCategory
295
375
MLIRScanner::VisitOMPSingleDirective (clang::OMPSingleDirective *par) {
296
376
IfScope scope (*this );
0 commit comments