Skip to content

Commit

Permalink
Merge pull request #399 from Xilinx/jose.fix_pass
Browse files Browse the repository at this point in the history
Make EliminateLibm work on EmitC::FuncOp
  • Loading branch information
mgehre-amd authored Nov 12, 2024
2 parents 213d2b0 + 1fc2b98 commit 2113e3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
6 changes: 6 additions & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,12 @@ def EmitC_FuncOp : EmitC_Op<"func", [

/// Returns the result types of this function.
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }

//===------------------------------------------------------------------===//
// SymbolOpInterface Methods
//===------------------------------------------------------------------===//

bool isDeclaration() { return isExternal(); }
}];
let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
Expand Down
10 changes: 5 additions & 5 deletions mlir/lib/Dialect/EmitC/Transforms/EliminateLibm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace {

/// Replace all Libm calls (where callee has `libm` attribute + no definition)
/// by opaque calls
struct OpacifyLibmCall : public OpRewritePattern<func::CallOp> {
using OpRewritePattern<func::CallOp>::OpRewritePattern;
LogicalResult matchAndRewrite(func::CallOp callOp,
struct OpacifyLibmCall : public OpRewritePattern<emitc::CallOp> {
using OpRewritePattern<emitc::CallOp>::OpRewritePattern;
LogicalResult matchAndRewrite(emitc::CallOp callOp,
PatternRewriter &rewriter) const override {

auto *st = SymbolTable::getNearestSymbolTable(callOp);
Expand All @@ -65,8 +65,8 @@ struct EliminateLibmPass : public impl::EliminateLibmBase<EliminateLibmPass> {
MLIRContext *context = module->getContext();

// Find the first math.h inclusion
SmallVector<func::FuncOp> libmPrototypes;
module.walk([&libmPrototypes](func::FuncOp funcOp) {
SmallVector<emitc::FuncOp> libmPrototypes;
module.walk([&libmPrototypes](emitc::FuncOp funcOp) {
if (funcOp->hasAttr("libm") && funcOp.isDeclaration())
libmPrototypes.push_back(funcOp);
});
Expand Down
26 changes: 13 additions & 13 deletions mlir/test/Dialect/EmitC/eliminate_libm.mlir
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// RUN: mlir-opt %s --eliminate-libm --verify-diagnostics --split-input-file | FileCheck %s

// CHECK: emitc.include <"cmath">
// CHECK-NOT: func.func private @expm1
// CHECK-DAG: func.func @call_expm1(%[[IN:.*]]: f64) -> f64
// CHECK-NOT: emitc.func private @expm1
// CHECK-DAG: emitc.func @call_expm1(%[[IN:.*]]: f64) -> f64
// CHECK-DAG: %[[RESULT:.*]] = emitc.call_opaque "expm1"(%[[IN]]) : (f64) -> f64
// CHECK-DAG: return %[[RESULT]]
module {
func.func private @expm1(f64) -> f64 attributes {libm, llvm.readnone}
func.func @call_expm1(%in : f64) -> f64 {
%e1 = func.call @expm1(%in) : (f64) -> f64
return %e1 : f64
emitc.func private @expm1(f64) -> f64 attributes {libm, llvm.readnone, specifiers = ["extern"]}
emitc.func @call_expm1(%in : f64) -> f64 {
%e1 = emitc.call @expm1(%in) : (f64) -> f64
emitc.return %e1 : f64
}
}

// -----

// CHECK-NOT: emitc.include <"cmath">
// CHECK: func.func private @expm1
// CHECK: func.func @call_expm1(%[[IN:.*]]: f64) -> f64
// CHECK-NEXT: %[[RESULT:.*]] = call @expm1(%[[IN]]) : (f64) -> f64
// CHECK: emitc.func private @expm1
// CHECK: emitc.func @call_expm1(%[[IN:.*]]: f64) -> f64
// CHECK-NEXT: %[[RESULT:.*]] = emitc.call @expm1(%[[IN]]) : (f64) -> f64
// CHECK-NEXT: return %[[RESULT]]
module {
func.func private @expm1(f64) -> f64 attributes {llvm.readnone}
func.func @call_expm1(%in : f64) -> f64 {
%e1 = func.call @expm1(%in) : (f64) -> f64
return %e1 : f64
emitc.func private @expm1(f64) -> f64 attributes {llvm.readnone}
emitc.func @call_expm1(%in : f64) -> f64 {
%e1 = emitc.call @expm1(%in) : (f64) -> f64
emitc.return %e1 : f64
}
}
8 changes: 4 additions & 4 deletions mlir/test/Dialect/EmitC/func.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ emitc.func @f(%x: i32 ref) {

// -----

// CHECK: emitc.func @f
// CHECK: emitc.func private @f
// CHECK-SAME: i32 ref
emitc.func @f(i32 ref)
emitc.func private @f(i32 ref)

// -----

// CHECK: emitc.func @f
// CHECK: emitc.func private @f
// CHECK-SAME: i32 ref
emitc.func @f(i32 {emitc.reference})
emitc.func private @f(i32 {emitc.reference})

0 comments on commit 2113e3c

Please sign in to comment.