llvm / llvm/Polygeist

Add Support for Additional Math Operations in Polygeist

Open
#407 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
624
Forks
170
PR merge metrics
No merged PRs in 30d

Description

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.

https://github.com/llvm/Polygeist/blob/8f8ae725ac6473749d38824e75611e07b6f99980/tools/cgeist/Lib/CGCall.cc#L979-L986

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

https://github.com/llvm/llvm-project/commit/b8dca4fa729fcbd5d42ce3ca056dc4d278da2548

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.

```c
// foo.c
#include

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:

```mlir
module attributes {} {
memref.global @buf : memref<100xf64> = uninitialized
func.func @main() -> i32 attributes {llvm.linkage = #llvm.linkage} {
%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}
func.func private @tanh(f64) -> f64 attributes {llvm.linkage = #llvm.linkage}
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in tools/cgeist/Lib/CGCall.cc at the linked lines and review how the existing math operations are transformed, along with the llvm-project submodule version. Run cgeist foo.c -function=main -S on the provided sample and verify that atanh and the requested additional operations produce the corresponding MLIR operations rather than external calls or an incorrect operation.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.