llvm / llvm/Polygeist

Enable passing function pointers as arguments.

Open
#211 1 comment 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

Hello,

The following code snippet :

````
int less () { .. }
int bsearch(int i,int (*compar)()) { .. }

int bug4(int i) {
int res = bsearch(1,less);
}
````

when running

````
Polygeist/build/bin/mlir-clang -S --raise-scf-to-affine -function=* -o=bug4.txt bug4.c
````

produces

````
clang version 15.0.0 (https://github.com/llvm/llvm-project.git 08ac66124874d70dab63c731da0244f9e29ef168)
...
loc("bug4.c":2:5): error: redefinition of symbol named 'less'
"builtin.module"() ({
"llvm.func"() ({
}) {function_type = !llvm.func, linkage = #llvm.linkage, sym_name = "less"} : () -> ()
"func.func"() ({
%0 = "arith.constant"() {value = 1 : i32} : () -> i32
"func.return"(%0) : (i32) -> ()
}) {function_type = () -> i32, llvm.linkage = #llvm.linkage, sym_name = "less"} : () -> ()
"func.func"() ({
^bb0(%arg0: i32, %arg1: !llvm.ptr>):
%0 = "arith.constant"() {value = 1 : i32} : () -> i32
%1 = "llvm.call"(%arg1) : (!llvm.ptr>) -> i32
"func.return"(%0) : (i32) -> ()
}) {function_type = (i32, !llvm.ptr>) -> i32, llvm.linkage = #llvm.linkage, sym_name = "bsearch"} : () -> ()
"func.func"() ({
^bb0(%arg0: i32):
%0 = "arith.constant"() {value = 1 : i32} : () -> i32
%1 = "llvm.mlir.addressof"() {global_name = @less} : () -> !llvm.ptr>
%2 = "func.call"(%0, %1) {callee = @bsearch} : (i32, !llvm.ptr>) -> i32
"func.return"(%2) : (i32) -> ()
}) {function_type = (i32) -> i32, llvm.linkage = #llvm.linkage, sym_name = "bug4"} : () -> ()
}) {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.endianness", "little">, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>, #dlti.dl_entry : vector<2xi32>>>, llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-unknown-linux-gnu"} : () -> ()
````

The C source are below

[bug4 .c.txt](https://github.com/wsmoses/Polygeist/files/8691926/bug4.c.txt)

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 by reproducing the command shown with the attached bug4.c source and inspect the emitted MLIR around the duplicate less symbols. Trace how Polygeist lowers the function definition and function-pointer argument. Done means the example compiles without a symbol redefinition and preserves the indirect call through the function pointer.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
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.