Skip to content

Commit 745d684

Browse files
authored
Foreach and prefix malloc/free (#199)
* Don't prefix malloc/free * C++ Foreach
1 parent 5f1b14f commit 745d684

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

tools/mlir-clang/Lib/CGStmt.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,86 @@ ValueCategory MLIRScanner::VisitForStmt(clang::ForStmt *fors) {
291291
return nullptr;
292292
}
293293

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+
294374
ValueCategory
295375
MLIRScanner::VisitOMPSingleDirective(clang::OMPSingleDirective *par) {
296376
IfScope scope(*this);

tools/mlir-clang/Lib/clang-mlir.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4006,7 +4006,8 @@ MLIRASTConsumer::GetOrCreateLLVMFunction(const FunctionDecl *FD) {
40064006
else
40074007
name = CGM.getMangledName(FD).str();
40084008

4009-
name = (PrefixABI + name);
4009+
if (name != "malloc" && name != "free")
4010+
name = (PrefixABI + name);
40104011

40114012
if (llvmFunctions.find(name) != llvmFunctions.end()) {
40124013
return llvmFunctions[name];

tools/mlir-clang/Lib/clang-mlir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ class MLIRScanner : public StmtVisitor<MLIRScanner, ValueCategory> {
263263

264264
ValueCategory VisitForStmt(clang::ForStmt *fors);
265265

266+
ValueCategory VisitCXXForRangeStmt(clang::CXXForRangeStmt *fors);
267+
266268
ValueCategory VisitOMPSingleDirective(clang::OMPSingleDirective *);
267269

268270
ValueCategory VisitOMPForDirective(clang::OMPForDirective *);

0 commit comments

Comments
 (0)