Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Additional Math Operations in Polygeist #407

Open
KashunCheng opened this issue May 9, 2024 · 0 comments
Open

Add Support for Additional Math Operations in Polygeist #407

KashunCheng opened this issue May 9, 2024 · 0 comments

Comments

@KashunCheng
Copy link

In the codebase of the Polygeist project, there's a discrepancy between the function atan and atanh. It appears that math::AtanhOp is the suitable counterpart for atanh. However, the current implementation utilizes math.atan, which seems incorrect.

if (sr->getDecl()->getIdentifier() &&
(sr->getDecl()->getName() == "__builtin_atanh" ||
sr->getDecl()->getName() == "__builtin_atanhf" ||
sr->getDecl()->getName() == "__builtin_atanhl")) {
mlir::Value V = getLLVM(expr->getArg(0));
V = builder.create<math::AtanOp>(loc, V);
return ValueCategory(V, /*isRef*/ false);
}

However, math::AtanhOp is introduced in a later commit of llvm-project, which is not available for the current submodule version.

llvm/llvm-project@b8dca4f

Furthermore, I've observed that Polygeist lacks support for several math operations, for example, math::TanhOP. I would like to implement support for new operations once I understand how the transformation works.

I've provided a sample code snippet below along with the generated MLIR code by Polygeist.

// foo.c
#include <math.h>

double buf[100];

int main(){
  for(int i=0;i<100;i++){
    buf[i] = sin(buf[i]);
    buf[i] = atanh(buf[i]);
    buf[i] = __builtin_atanh(buf[i]);
    buf[i] = tanh(buf[i]);
  }
}

Current MLIR output by executing cgeist foo.c -function=main -S for the provided code snippet:

module attributes {<omitted>} {
  memref.global @buf : memref<100xf64> = uninitialized
  func.func @main() -> i32 attributes {llvm.linkage = #llvm.linkage<external>} {
    %c100 = arith.constant 100 : index
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %0 = llvm.mlir.undef : i32
    %1 = memref.get_global @buf : memref<100xf64>
    scf.for %arg0 = %c0 to %c100 step %c1 {
      %2 = memref.load %1[%arg0] : memref<100xf64>
      %3 = math.sin %2 : f64
      memref.store %3, %1[%arg0] : memref<100xf64>
      %4 = func.call @atanh(%3) : (f64) -> f64
      memref.store %4, %1[%arg0] : memref<100xf64>
      %5 = math.atan %4 : f64
      memref.store %5, %1[%arg0] : memref<100xf64>
      %6 = func.call @tanh(%5) : (f64) -> f64
      memref.store %6, %1[%arg0] : memref<100xf64>
    }
    return %0 : i32
  }
  func.func private @atanh(f64) -> f64 attributes {llvm.linkage = #llvm.linkage<external>}
  func.func private @tanh(f64) -> f64 attributes {llvm.linkage = #llvm.linkage<external>}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant