Skip to content

Commit f73f194

Browse files
authored
Improve debug info (#256)
* Improve debug info * More debug info * Preserve pass locations
1 parent efc0215 commit f73f194

File tree

12 files changed

+401
-322
lines changed

12 files changed

+401
-322
lines changed

lib/polygeist/Passes/BarrierRemovalContinuation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static void splitBlocksWithBarrier(Region &region) {
105105
Block *original = op->getBlock();
106106
Block *block = original->splitBlock(op->getNextNode());
107107
auto builder = OpBuilder::atBlockEnd(original);
108-
builder.create<cf::BranchOp>(builder.getUnknownLoc(), block);
108+
builder.create<cf::BranchOp>(op.getLoc(), block);
109109
}
110110
}
111111

lib/polygeist/Passes/CanonicalizeFor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ struct MoveWhileToFor : public OpRewritePattern<WhileOp> {
922922
} else
923923
res = arg;
924924
if (cst) {
925-
res = rewriter.create<IndexCastOp>(rewriter.getUnknownLoc(), cst, res);
925+
res = rewriter.create<IndexCastOp>(res.getLoc(), cst, res);
926926
}
927927
forArgs.push_back(res);
928928
}

lib/polygeist/Passes/LoopRestructure.cpp

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ bool LoopRestructure::removeIfFromRegion(DominanceInfo &domInfo, Region &region,
292292
if (Succs[j] == pseudoExit && Succs[1 - j] == Preds[1 - i]) {
293293
OpBuilder builder(Preds[i]->getTerminator());
294294
auto condBr = cast<cf::CondBranchOp>(Preds[i]->getTerminator());
295-
auto ifOp = builder.create<scf::IfOp>(
296-
builder.getUnknownLoc(), condTys, condBr.getCondition(),
297-
/*hasElse*/ true);
295+
auto ifOp = builder.create<scf::IfOp>(condBr.getLoc(), condTys,
296+
condBr.getCondition(),
297+
/*hasElse*/ true);
298298
Succs[j] = new Block();
299299
if (j == 0) {
300300
ifOp.getElseRegion().getBlocks().splice(
@@ -311,7 +311,7 @@ bool LoopRestructure::removeIfFromRegion(DominanceInfo &domInfo, Region &region,
311311
assert(condTys.size() == condBr.getTrueOperands().size());
312312
OpBuilder tbuilder(&ifOp.getThenRegion().front(),
313313
ifOp.getThenRegion().front().begin());
314-
tbuilder.create<scf::YieldOp>(tbuilder.getUnknownLoc(), emptyTys,
314+
tbuilder.create<scf::YieldOp>(condBr.getLoc(), emptyTys,
315315
condBr.getTrueOperands());
316316
} else {
317317
if (!ifOp.getThenRegion().getBlocks().empty()) {
@@ -331,17 +331,16 @@ bool LoopRestructure::removeIfFromRegion(DominanceInfo &domInfo, Region &region,
331331
OpBuilder tbuilder(&ifOp.getElseRegion().front(),
332332
ifOp.getElseRegion().front().begin());
333333
assert(condTys.size() == condBr.getFalseOperands().size());
334-
tbuilder.create<scf::YieldOp>(tbuilder.getUnknownLoc(), emptyTys,
334+
tbuilder.create<scf::YieldOp>(condBr.getLoc(), emptyTys,
335335
condBr.getFalseOperands());
336336
}
337337
auto *oldTerm = Succs[1 - j]->getTerminator();
338338
OpBuilder tbuilder(Succs[1 - j], Succs[1 - j]->end());
339-
tbuilder.create<scf::YieldOp>(tbuilder.getUnknownLoc(), emptyTys,
339+
tbuilder.create<scf::YieldOp>(condBr.getLoc(), emptyTys,
340340
oldTerm->getOperands());
341341
oldTerm->erase();
342342

343-
builder.create<scf::YieldOp>(builder.getUnknownLoc(),
344-
ifOp->getResults());
343+
builder.create<scf::YieldOp>(condBr.getLoc(), ifOp->getResults());
345344
condBr->erase();
346345

347346
pseudoExit->erase();
@@ -385,9 +384,10 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
385384

386385
// Copy the arguments across
387386
SmallVector<Type, 4> headerArgumentTypes(header->getArgumentTypes());
388-
SmallVector<Location> locs(headerArgumentTypes.size(),
389-
builder.getUnknownLoc());
390-
wrapper->addArguments(headerArgumentTypes, locs);
387+
SmallVector<Location, 4> headerArgumentLocs;
388+
for (auto a : header->getArguments())
389+
headerArgumentLocs.push_back(a.getLoc());
390+
wrapper->addArguments(headerArgumentTypes, headerArgumentLocs);
391391

392392
SmallVector<Value> valsCallingLoop;
393393
for (auto a : wrapper->getArguments())
@@ -405,20 +405,28 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
405405
})) {
406406
preservedVals.emplace_back(V, headerArgumentTypes.size());
407407
headerArgumentTypes.push_back(V.getType());
408-
valsCallingLoop.push_back(builder.create<mlir::LLVM::UndefOp>(
409-
builder.getUnknownLoc(), V.getType()));
408+
headerArgumentLocs.push_back(V.getLoc());
409+
valsCallingLoop.push_back(
410+
builder.create<mlir::LLVM::UndefOp>(V.getLoc(), V.getType()));
410411
header->addArgument(V.getType(), V.getLoc());
411412
}
412413
}
413414
}
414415
}
415416

416417
SmallVector<Type, 4> combinedTypes = headerArgumentTypes;
418+
SmallVector<Location, 4> combinedLocs = headerArgumentLocs;
419+
417420
SmallVector<Type, 4> returns(target->getArgumentTypes());
421+
SmallVector<Location, 4> returnLocs;
422+
for (auto a : target->getArguments())
423+
returnLocs.push_back(a.getLoc());
418424
combinedTypes.append(returns);
425+
combinedLocs.append(returnLocs);
426+
assert(combinedTypes.size() == combinedLocs.size());
419427

420428
auto loop = builder.create<mlir::scf::WhileOp>(
421-
builder.getUnknownLoc(), combinedTypes, valsCallingLoop);
429+
header->front().getLoc(), combinedTypes, valsCallingLoop);
422430
{
423431
SmallVector<Value, 4> RetVals;
424432
for (size_t i = 0; i < returns.size(); ++i) {
@@ -449,15 +457,18 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
449457
SmallVector<Type, 4> tys = {builder.getI1Type()};
450458
for (auto t : combinedTypes)
451459
tys.push_back(t);
452-
auto exec =
453-
builder.create<scf::ExecuteRegionOp>(builder.getUnknownLoc(), tys);
460+
SmallVector<Location, 4> locs = {region.getLoc()};
461+
for (auto t : combinedLocs)
462+
locs.push_back(t);
463+
assert(tys.size() == locs.size());
464+
auto exec = builder.create<scf::ExecuteRegionOp>(region.getLoc(), tys);
454465

455466
{
456467
SmallVector<Value> yields;
457468
for (auto a : exec.getResults())
458469
yields.push_back(a);
459470
yields.erase(yields.begin());
460-
builder.create<scf::ConditionOp>(builder.getUnknownLoc(),
471+
builder.create<scf::ConditionOp>(header->getTerminator()->getLoc(),
461472
exec.getResult(0), yields);
462473
}
463474

@@ -477,10 +488,10 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
477488
Block *pseudoExit = new Block();
478489
{
479490
insertRegion.push_back(pseudoExit);
480-
SmallVector<Location> locs(tys.size(), builder.getUnknownLoc());
481491
pseudoExit->addArguments(tys, locs);
482492
OpBuilder builder(pseudoExit, pseudoExit->begin());
483493
tys.clear();
494+
locs.clear();
484495
builder.create<scf::YieldOp>(builder.getUnknownLoc(), tys,
485496
pseudoExit->getArguments());
486497
}
@@ -493,8 +504,8 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
493504
if (successor == target) {
494505

495506
OpBuilder builder(terminator);
496-
auto vfalse = builder.create<arith::ConstantIntOp>(
497-
builder.getUnknownLoc(), false, 1);
507+
auto vfalse =
508+
builder.create<arith::ConstantIntOp>(region.getLoc(), false, 1);
498509

499510
SmallVector<Value> args = {vfalse};
500511
for (auto arg : header->getArguments())
@@ -547,17 +558,17 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
547558
if (successor == header) {
548559

549560
OpBuilder builder(terminator);
550-
auto vtrue = builder.create<arith::ConstantIntOp>(
551-
builder.getUnknownLoc(), true, 1);
561+
auto vtrue =
562+
builder.create<arith::ConstantIntOp>(region.getLoc(), true, 1);
552563

553564
if (auto op = dyn_cast<cf::BranchOp>(terminator)) {
554565
SmallVector<Value> args(op.getOperands());
555566
args.insert(args.begin(), vtrue);
556567
for (auto p : preservedVals)
557568
args.push_back(p.first);
558-
for (auto ty : returns) {
569+
for (auto tup : llvm::zip(returns, returnLocs)) {
559570
args.push_back(builder.create<mlir::LLVM::UndefOp>(
560-
builder.getUnknownLoc(), ty));
571+
std::get<1>(tup), std::get<0>(tup)));
561572
}
562573
terminator =
563574
builder.create<cf::BranchOp>(op.getLoc(), pseudoExit, args);
@@ -571,18 +582,18 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
571582
trueargs.insert(trueargs.begin(), vtrue);
572583
for (auto pair : preservedVals)
573584
trueargs.push_back(pair.first);
574-
for (auto ty : returns) {
585+
for (auto tup : llvm::zip(returns, returnLocs)) {
575586
trueargs.push_back(builder.create<mlir::LLVM::UndefOp>(
576-
builder.getUnknownLoc(), ty));
587+
std::get<1>(tup), std::get<0>(tup)));
577588
}
578589
}
579590
if (op.getFalseDest() == header) {
580591
falseargs.insert(falseargs.begin(), vtrue);
581592
for (auto pair : preservedVals)
582593
falseargs.push_back(pair.first);
583-
for (auto ty : returns) {
594+
for (auto tup : llvm::zip(returns, returnLocs)) {
584595
falseargs.push_back(builder.create<mlir::LLVM::UndefOp>(
585-
builder.getUnknownLoc(), ty));
596+
std::get<1>(tup), std::get<0>(tup)));
586597
}
587598
}
588599
// Recreate the terminator and store it so that its other
@@ -600,8 +611,7 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
600611
}
601612

602613
Block *after = new Block();
603-
SmallVector<Location> locs2(combinedTypes.size(), region.getLoc());
604-
after->addArguments(combinedTypes, locs2);
614+
after->addArguments(combinedTypes, combinedLocs);
605615
loop.getAfter().push_back(after);
606616
OpBuilder builder2(after, after->begin());
607617
SmallVector<Value, 4> yieldargs;
@@ -637,7 +647,8 @@ void LoopRestructure::runOnRegion(DominanceInfo &domInfo, Region &region) {
637647
}
638648
header->eraseArguments([](BlockArgument) { return true; });
639649

640-
builder2.create<scf::YieldOp>(builder.getUnknownLoc(), yieldargs);
650+
builder2.create<scf::YieldOp>(header->getTerminator()->getLoc(),
651+
yieldargs);
641652
domInfo.invalidate(&insertRegion);
642653

643654
assert(header->getParent() == &insertRegion);

lib/polygeist/Passes/ParallelLower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ void ParallelLower::runOnOperation() {
268268
callInliner(op);
269269

270270
mlir::IRRewriter builder(launchOp.getContext());
271-
auto loc = builder.getUnknownLoc();
271+
auto loc = launchOp.getLoc();
272272

273273
builder.setInsertionPoint(launchOp->getBlock(), launchOp->getIterator());
274274
auto zindex = builder.create<ConstantIndexOp>(loc, 0);

tools/cgeist/Lib/CGCall.cc

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ ValueCategory MLIRScanner::CallHelper(
8484
SmallVector<mlir::Value, 4> args;
8585
auto fnType = tocall.getFunctionType();
8686

87+
auto loc = getMLIRLocation(expr->getExprLoc());
88+
8789
size_t i = 0;
8890
// map from declaration name to mlir::value
8991
std::map<std::string, mlir::Value> mapFuncOperands;
@@ -152,7 +154,7 @@ ValueCategory MLIRScanner::CallHelper(
152154
MemRefLayoutAttrInterface(),
153155
mt.getMemorySpace()));
154156
ValueCategory(alloc, /*isRef*/ true)
155-
.store(builder, arg, /*isArray*/ isArray);
157+
.store(loc, builder, arg, /*isArray*/ isArray);
156158
shape[0] = pshape;
157159
val = builder.create<mlir::memref::CastOp>(
158160
loc,
@@ -161,7 +163,7 @@ ValueCategory MLIRScanner::CallHelper(
161163
mt.getMemorySpace()),
162164
alloc);
163165
} else {
164-
val = arg.getValue(builder);
166+
val = arg.getValue(loc, builder);
165167
if (val.getType().isa<LLVM::LLVMPointerType>() &&
166168
expectedType.isa<MemRefType>()) {
167169
val = builder.create<polygeist::Pointer2MemrefOp>(loc, expectedType,
@@ -300,7 +302,7 @@ ValueCategory MLIRScanner::CallHelper(
300302
SmallVector<mlir::Value, 1> asyncDependencies;
301303
if (3 < CU->getConfig()->getNumArgs() &&
302304
!isa<CXXDefaultArgExpr>(CU->getConfig()->getArg(3))) {
303-
stream = Visit(CU->getConfig()->getArg(3)).getValue(builder);
305+
stream = Visit(CU->getConfig()->getArg(3)).getValue(loc, builder);
304306
stream = builder.create<polygeist::StreamToTokenOp>(
305307
loc, builder.getType<gpu::AsyncTokenType>(), stream);
306308
assert(stream);
@@ -367,7 +369,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
367369
sr->getDecl()->getName() == "__shfl_up_sync") {
368370
std::vector<mlir::Value> args;
369371
for (auto a : expr->arguments()) {
370-
args.push_back(Visit(a).getValue(builder));
372+
args.push_back(Visit(a).getValue(loc, builder));
371373
}
372374
builder.create<gpu::ShuffleOp>(loc, );
373375
assert(0 && "__shfl_up_sync unhandled");
@@ -421,9 +423,10 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
421423
while (auto *CE = dyn_cast<llvm::ConstantExpr>(LC))
422424
LC = CE->getOperand(0);
423425
std::string val = cast<llvm::GlobalVariable>(LC)->getName().str();
424-
return CommonArrayToPointer(ValueCategory(
425-
Glob.GetOrCreateGlobalLLVMString(loc, builder, val),
426-
/*isReference*/ true));
426+
return CommonArrayToPointer(
427+
loc, ValueCategory(
428+
Glob.GetOrCreateGlobalLLVMString(loc, builder, val),
429+
/*isReference*/ true));
427430
}
428431
}
429432
}
@@ -443,8 +446,8 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
443446
for (auto *a : expr->arguments()) {
444447
args.push_back(Visit(a));
445448
}
446-
auto a0 = args[0].getValue(builder);
447-
auto a1 = args[1].getValue(builder);
449+
auto a0 = args[0].getValue(loc, builder);
450+
auto a1 = args[1].getValue(loc, builder);
448451
AtomicRMWKind op;
449452
LLVM::AtomicBinOp lop;
450453
if (sr->getDecl()->getName() == "atomicAdd") {
@@ -518,10 +521,10 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
518521
0),
519522
one, 0);
520523
ValueCategory(alloc, /*isRef*/ true)
521-
.store(builder, sub, /*isArray*/ isArray);
524+
.store(loc, builder, sub, /*isArray*/ isArray);
522525
sub = ValueCategory(alloc, /*isRef*/ true);
523526
}
524-
auto val = sub.getValue(builder);
527+
auto val = sub.getValue(loc, builder);
525528
if (auto mt = val.getType().dyn_cast<MemRefType>()) {
526529
auto nt = Glob.typeTranslator
527530
.translateType(anonymize(getLLVMType(E->getType())))
@@ -546,7 +549,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
546549
auto mlirType = getMLIRType(expr->getType());
547550
std::vector<mlir::Value> args;
548551
for (auto *a : expr->arguments()) {
549-
args.push_back(Visit(a).getValue(builder));
552+
args.push_back(Visit(a).getValue(loc, builder));
550553
}
551554
if (args[1].getType().isa<mlir::IntegerType>())
552555
return ValueCategory(
@@ -1089,7 +1092,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
10891092
mlir::Value dst;
10901093
ValueCategory vdst = Visit(dstSub);
10911094
if (isa<clang::PointerType>(dstst)) {
1092-
dst = vdst.getValue(builder);
1095+
dst = vdst.getValue(loc, builder);
10931096
} else {
10941097
assert(vdst.isReference);
10951098
dst = vdst.val;
@@ -1099,7 +1102,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
10991102
mlir::Value src;
11001103
ValueCategory vsrc = Visit(srcSub);
11011104
if (isa<clang::PointerType>(srcst)) {
1102-
src = vsrc.getValue(builder);
1105+
src = vsrc.getValue(loc, builder);
11031106
} else {
11041107
assert(vsrc.isReference);
11051108
src = vsrc.val;
@@ -1113,13 +1116,13 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
11131116
if (srcArray && !dstArray)
11141117
elemSize = getTypeSize(QualType(selem, 0));
11151118
mlir::Value size = builder.create<IndexCastOp>(
1116-
loc, Visit(expr->getArg(2)).getValue(builder),
1119+
loc, Visit(expr->getArg(2)).getValue(loc, builder),
11171120
mlir::IndexType::get(builder.getContext()));
11181121
size = builder.create<DivUIOp>(
11191122
loc, size, builder.create<ConstantIndexOp>(loc, elemSize));
11201123

11211124
if (sr->getDecl()->getName() == "cudaMemcpyToSymbol") {
1122-
mlir::Value offset = Visit(expr->getArg(3)).getValue(builder);
1125+
mlir::Value offset = Visit(expr->getArg(3)).getValue(loc, builder);
11231126
offset = builder.create<IndexCastOp>(
11241127
loc, offset, mlir::IndexType::get(builder.getContext()));
11251128
offset = builder.create<DivUIOp>(
@@ -1293,7 +1296,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
12931296
mlir::Value dst;
12941297
ValueCategory vdst = Visit(dstSub);
12951298
if (isa<clang::PointerType>(dstst)) {
1296-
dst = vdst.getValue(builder);
1299+
dst = vdst.getValue(loc, builder);
12971300
} else {
12981301
assert(vdst.isReference);
12991302
dst = vdst.val;
@@ -1313,7 +1316,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
13131316

13141317
auto elemSize = getTypeSize(elem);
13151318
mlir::Value size = builder.create<IndexCastOp>(
1316-
loc, Visit(expr->getArg(2)).getValue(builder),
1319+
loc, Visit(expr->getArg(2)).getValue(loc, builder),
13171320
mlir::IndexType::get(builder.getContext()));
13181321
size = builder.create<DivUIOp>(
13191322
loc, size, builder.create<ConstantIndexOp>(loc, elemSize));
@@ -1504,7 +1507,7 @@ ValueCategory MLIRScanner::VisitCallExpr(clang::CallExpr *expr) {
15041507
CC->getImplicitObjectArgument()->dump();
15051508
}
15061509
if (cast<MemberExpr>(CC->getCallee()->IgnoreParens())->isArrow()) {
1507-
obj = obj.dereference(builder);
1510+
obj = obj.dereference(loc, builder);
15081511
}
15091512
assert(obj.val);
15101513
assert(obj.isReference);

0 commit comments

Comments
 (0)