Skip to content

Commit 45a10b9

Browse files
authored
Fix mem2reg issue (#292)
1 parent bad9554 commit 45a10b9

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

lib/polygeist/Ops.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ bool collectEffects(Operation *op,
104104

105105
return true;
106106
}
107+
if (*callee == "fscanf" || *callee == "__isoc99_fscanf") {
108+
// Global read
109+
effects.emplace_back(MemoryEffects::Effect::get<MemoryEffects::Read>());
110+
111+
for (auto argp : llvm::enumerate(cop.getArgOperands())) {
112+
auto arg = argp.value();
113+
auto idx = argp.index();
114+
if (idx == 0) {
115+
effects.emplace_back(::mlir::MemoryEffects::Read::get(), arg,
116+
::mlir::SideEffects::DefaultResource::get());
117+
effects.emplace_back(::mlir::MemoryEffects::Write::get(), arg,
118+
::mlir::SideEffects::DefaultResource::get());
119+
} else if (idx == 1) {
120+
effects.emplace_back(::mlir::MemoryEffects::Read::get(), arg,
121+
::mlir::SideEffects::DefaultResource::get());
122+
} else
123+
effects.emplace_back(::mlir::MemoryEffects::Write::get(), arg,
124+
::mlir::SideEffects::DefaultResource::get());
125+
}
126+
127+
return true;
128+
}
107129
if (*callee == "printf") {
108130
// Global read
109131
effects.emplace_back(

lib/polygeist/Passes/Mem2Reg.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,12 @@ void removeRedundantBlockArgs(
10671067
}
10681068

10691069
std::set<std::string> NonCapturingFunctions = {
1070-
"free", "printf", "fprintf", "scanf", "fscanf",
1071-
"gettimeofday", "clock_gettime", "getenv", "strrchr", "strlen",
1072-
"sprintf", "sscanf", "mkdir", "fwrite", "fread",
1073-
"memcpy", "cudaMemcpy", "memset", "cudaMemset", "__isoc99_scanf"};
1070+
"free", "printf", "fprintf", "scanf",
1071+
"fscanf", "gettimeofday", "clock_gettime", "getenv",
1072+
"strrchr", "strlen", "sprintf", "sscanf",
1073+
"mkdir", "fwrite", "fread", "memcpy",
1074+
"cudaMemcpy", "memset", "cudaMemset", "__isoc99_scanf",
1075+
"__isoc99_fscanf"};
10741076
// fopen, fclose
10751077
std::set<std::string> NoWriteFunctions = {"exit", "__errno_location"};
10761078
// This is a straightforward implementation not optimized for speed. Optimize
@@ -1816,15 +1818,19 @@ bool isPromotable(mlir::Value AI) {
18161818
continue;
18171819
} else if (isa<memref::DeallocOp>(U)) {
18181820
continue;
1819-
} else if (isa<func::CallOp>(U) &&
1820-
cast<func::CallOp>(U).getCallee() == "free") {
1821-
continue;
1822-
} else if (isa<func::CallOp>(U)) {
1823-
// TODO check "no capture", currently assume as a fallback always
1824-
// nocapture
1825-
continue;
1821+
} else if (auto callOp = dyn_cast<func::CallOp>(U)) {
1822+
if (NonCapturingFunctions.count(callOp.getCallee().str()))
1823+
continue;
1824+
} else if (auto callOp = dyn_cast<LLVM::CallOp>(U)) {
1825+
if (auto callee = callOp.getCallee())
1826+
if (NonCapturingFunctions.count(callee->str()))
1827+
continue;
18261828
} else if (auto CO = dyn_cast<memref::CastOp>(U)) {
18271829
list.push_back(CO);
1830+
} else if (auto CO = dyn_cast<polygeist::Memref2PointerOp>(U)) {
1831+
list.push_back(CO);
1832+
} else if (auto CO = dyn_cast<polygeist::Pointer2MemrefOp>(U)) {
1833+
list.push_back(CO);
18281834
} else {
18291835
LLVM_DEBUG(llvm::dbgs()
18301836
<< "non promotable " << AI << " due to " << *U << "\n");

test/polygeist-opt/promoteonscan.mlir

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: polygeist-opt --mem2reg --split-input-file %s | FileCheck %s
2+
3+
module {
4+
llvm.func @scanf(!llvm.ptr<i8>, ...) -> i32
5+
llvm.mlir.global internal constant @str4("%d\00") {addr_space = 0 : i32}
6+
func.func @_Z8BFSGraphiPPc(%arg0: i32, %arg1: memref<?xmemref<?xi8>>) -> (i32, i32) {
7+
%c0_i32 = arith.constant 0 : i32
8+
%0 = llvm.mlir.undef : i32
9+
%alloca = memref.alloca() : memref<1xi32>
10+
affine.store %0, %alloca[0] : memref<1xi32>
11+
affine.store %c0_i32, %alloca[0] : memref<1xi32>
12+
%4 = affine.load %arg1[1] : memref<?xmemref<?xi8>>
13+
%8 = llvm.mlir.addressof @str4 : !llvm.ptr<array<3 x i8>>
14+
%9 = llvm.getelementptr %8[0, 0] : (!llvm.ptr<array<3 x i8>>) -> !llvm.ptr<i8>
15+
%10 = "polygeist.memref2pointer"(%alloca) : (memref<1xi32>) -> !llvm.ptr<i32>
16+
%11 = llvm.call @scanf(%9, %10) : (!llvm.ptr<i8>, !llvm.ptr<i32>) -> i32
17+
%12 = affine.load %alloca[0] : memref<1xi32>
18+
%13 = affine.load %alloca[0] : memref<1xi32>
19+
return %13, %12 : i32, i32
20+
}
21+
// CHECK: %[[i4:.+]] = "polygeist.memref2pointer"(%[[alloca:.+]]) : (memref<1xi32>) -> !llvm.ptr<i32>
22+
// CHECK-NEXT: %[[i5:.+]] = llvm.call @scanf(%[[i3:.+]], %[[i4]]) : (!llvm.ptr<i8>, !llvm.ptr<i32>) -> i32
23+
// CHECK-NEXT: %[[i6:.+]] = affine.load %[[alloca]][0] : memref<1xi32>
24+
// CHECK-NEXT: return %[[i6]], %[[i6]] : i32, i32
25+
}
26+

tools/cgeist/Test/Verification/fscanf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ int* alloc() {
4040
// CHECK-NEXT: %[[V7:.+]] = arith.index_cast %[[V6]] : i64 to index
4141
// CHECK-NEXT: %[[V8:.+]] = arith.divui %[[V7]], %[[c4]] : index
4242
// CHECK-NEXT: %[[i8:.+]] = memref.alloc(%[[V8]]) : memref<?xi32>
43+
// CHECK-NEXT: %[[n:.+]] = arith.index_cast %[[V4]] : i32 to index
4344
// CHECK-NEXT: %[[i9:.+]] = llvm.mlir.addressof @str1 : !llvm.ptr<array<4 x i8>>
4445
// CHECK-NEXT: %[[i10:.+]] = llvm.getelementptr %[[i9]][0, 0] : (!llvm.ptr<array<4 x i8>>) -> !llvm.ptr<i8>
4546
// CHECK-NEXT: %[[V12:.+]] = "polygeist.memref2pointer"(%[[i8]]) : (memref<?xi32>) -> !llvm.ptr<i32>
46-
// CHECK-NEXT: %[[n:.+]] = arith.index_cast %[[V4]] : i32 to index
4747
// CHECK-NEXT: scf.for %[[arg0:.+]] = %[[c0]] to %[[n]] step %[[c1]] {
4848
// CHECK-NEXT: %[[i14:.+]] = arith.index_cast %[[arg0]] : index to i64
4949
// CHECK-NEXT: %[[i15:.+]] = llvm.getelementptr %[[V12]][%[[i14]]] : (!llvm.ptr<i32>, i64) -> !llvm.ptr<i32>

0 commit comments

Comments
 (0)