llvm / llvm/Polygeist

Question: Without the cast operation, malloc cannot be correctly identified.

Open
#419 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

```cpp
void Cast1() {
void *mem = malloc(100);
char *arr = (char *)mem;
arr[0] = '%'; arr[1] = '#'; arr[2] = '\0';
printf("%s\n" , arr);
free(mem);
}

/*
func.func @Cast1() attributes {llvm.linkage = #llvm.linkage} {
%c0_i8 = arith.constant 0 : i8
%c35_i8 = arith.constant 35 : i8
%c37_i8 = arith.constant 37 : i8
%c100_i64 = arith.constant 100 : i64
%0 = call @malloc(%c100_i64) : (i64) -> memref
affine.store %c37_i8, %0[0] : memref
affine.store %c35_i8, %0[1] : memref
affine.store %c0_i8, %0[2] : memref
%1 = llvm.mlir.addressof @str0 : !llvm.ptr
%2 = llvm.getelementptr %1[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<4 x i8>
%3 = "polygeist.memref2pointer"(%0) : (memref) -> !llvm.ptr
%4 = llvm.call @printf(%2, %3) vararg(!llvm.func) : (!llvm.ptr, !llvm.ptr) -> i32
memref.dealloc %0 : memref
return
}
func.func private @malloc(i64) -> memref attributes {llvm.linkage = #llvm.linkage}
*/
```

I noticed that your malloc conversion is done in VisitCastExpr, so you can only recognize malloc as a custom function if you have a special case like above.

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 reading the malloc conversion logic in VisitCastExpr and compare it with the provided Cast1 example, where malloc is used without a cast. Trace how the call is represented and determine the expected behavior for recognizing malloc as a custom function without relying on a special cast case; done means the intended behavior is implemented or clearly documented with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
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.