Mem2Reg on LLVM

This commit is contained in:
William S. Moses 2021-12-23 21:39:56 -05:00 committed by William Moses
parent 37a338926a
commit b031af8430
4 changed files with 28 additions and 8 deletions

View File

@ -10,7 +10,7 @@ def AffineCFG : FunctionPass<"affine-cfg"> {
def Mem2Reg : FunctionPass<"mem2reg"> { def Mem2Reg : FunctionPass<"mem2reg"> {
let summary = "Replace scf.if and similar with affine.if"; let summary = "Replace scf.if and similar with affine.if";
let constructor = "mlir::polygeist::replaceAffineCFGPass()"; let constructor = "mlir::polygeist::createMem2RegPass()";
} }
def ParallelLower : Pass<"parallel-lower", "mlir::ModuleOp"> { def ParallelLower : Pass<"parallel-lower", "mlir::ModuleOp"> {

View File

@ -1344,10 +1344,12 @@ StoreMap getLastStored(mlir::Value AI) {
} }
} }
lastStored.insert(vec); lastStored.insert(vec);
} else if (auto LO = dyn_cast<LLVM::LoadOp>(U)) { } else if (isa<LLVM::LoadOp>(U)) {
lastStored.insert({}); std::vector<ssize_t> vec;
} else if (auto SO = dyn_cast<LLVM::StoreOp>(U)) { lastStored.insert(vec);
lastStored.insert({}); } else if (isa<LLVM::StoreOp>(U)) {
std::vector<ssize_t> vec;
lastStored.insert(vec);
} else if (auto SO = dyn_cast<memref::LoadOp>(U)) { } else if (auto SO = dyn_cast<memref::LoadOp>(U)) {
std::vector<ssize_t> vec; std::vector<ssize_t> vec;
for (auto idx : SO.getIndices()) { for (auto idx : SO.getIndices()) {
@ -1419,7 +1421,6 @@ void Mem2Reg::runOnFunction() {
LLVM_DEBUG(llvm::dbgs() << " attempting to promote " << AI << "\n"); LLVM_DEBUG(llvm::dbgs() << " attempting to promote " << AI << "\n");
auto lastStored = getLastStored(AI); auto lastStored = getLastStored(AI);
for (auto &vec : lastStored) { for (auto &vec : lastStored) {
LLVM_DEBUG(llvm::dbgs() << " + forwarding vec to promote {"; LLVM_DEBUG(llvm::dbgs() << " + forwarding vec to promote {";
for (auto m for (auto m
: vec) llvm::dbgs() : vec) llvm::dbgs()
@ -1462,7 +1463,8 @@ void Mem2Reg::runOnFunction() {
list.pop_front(); list.pop_front();
for (auto U : val.getUsers()) { for (auto U : val.getUsers()) {
if (isa<memref::StoreOp, AffineStoreOp, memref::DeallocOp>(U)) { if (isa<LLVM::StoreOp, memref::StoreOp, AffineStoreOp,
memref::DeallocOp>(U)) {
toErase.push_back(U); toErase.push_back(U);
} else if (isa<CallOp>(U) && cast<CallOp>(U).callee() == "free") { } else if (isa<CallOp>(U) && cast<CallOp>(U).callee() == "free") {
toErase.push_back(U); toErase.push_back(U);

View File

@ -0,0 +1,16 @@
// RUN: polygeist-opt --mem2reg --split-input-file %s | FileCheck %s
module {
func @ll(%arg0: !llvm.ptr<i8>) -> !llvm.ptr<i8> {
%c1_i64 = arith.constant 1 : i64
%2 = llvm.alloca %c1_i64 x !llvm.ptr<i8> : (i64) -> !llvm.ptr<ptr<i8>>
llvm.store %arg0, %2 : !llvm.ptr<ptr<i8>>
%3 = llvm.load %2 : !llvm.ptr<ptr<i8>>
return %3 : !llvm.ptr<i8>
}
}
// CHECK: func @ll(%arg0: !llvm.ptr<i8>) -> !llvm.ptr<i8> {
// CHECK-NEXT: %c1_i64 = arith.constant 1 : i64
// CHECK-NEXT: return %arg0 : !llvm.ptr<i8>
// CHECK-NEXT: }

View File

@ -1,5 +1,6 @@
// RUN: mlir-clang %s --function=foo -S | FileCheck %s // RUN: mlir-clang %s --function=foo -S | FileCheck %s
extern "C" {
int foo(char t) { int foo(char t) {
int n = 10; int n = 10;
switch (t) { switch (t) {
@ -14,9 +15,10 @@ int foo(char t) {
} }
return n; return n;
} }
}
// TODO the select should be canonicalized better // TODO the select should be canonicalized better
// CHECK: func @foo(%arg0: i32) -> i32 attributes {llvm.linkage = #llvm.linkage<external>} { // CHECK: func @foo(%arg0: i8) -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
// CHECK-NEXT: %c-1_i32 = arith.constant -1 : i32 // CHECK-NEXT: %c-1_i32 = arith.constant -1 : i32
// CHECK-NEXT: %c30_i32 = arith.constant 30 : i32 // CHECK-NEXT: %c30_i32 = arith.constant 30 : i32
// CHECK-NEXT: %false = arith.constant false // CHECK-NEXT: %false = arith.constant false