llvm / llvm/llvm-project

[CIR] CIR drops pointee const value qualifier

Open
#217,687 1 comment 0 reactions 0 assignees View on GitHub
ClangIR
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Take this program:

```c
int strcmp(const char *, const char *);
int (*p)(const char *, const char *) = strcmp;
```

Running the following:

```sh
~/llvm-project/build-cir/bin/clang -std=gnu11 -fclangir -emit-cir test.c -o -
```

I got this:

```mlir
!s32i = !cir.int
!s8i = !cir.int
...
cir.global external dso_local @p = #cir.global_view<@strcmp> : !cir.ptr, !cir.ptr) -> !s32i>> {alignment = 8 : i64} loc(#loc5)
cir.func private @strcmp(!cir.ptr {llvm.noundef}, !cir.ptr {llvm.noundef}) -> !s32i attributes {"cir.target-cpu" = "x86-64", "cir.target-features" = "+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87", "cir.tune-cpu" = "generic", nothrow} loc(#loc6)
} loc(#loc)
#loc = loc("/home/takashi/scratch/test.c":0:0)
#loc1 = loc("test.c":3:1)
#loc2 = loc("test.c":4:63)
#loc3 = loc("test.c":1:1)
#loc4 = loc("test.c":1:38)
#loc5 = loc(fused[#loc1, #loc2])
#loc6 = loc(fused[#loc3, #loc4])
```

Taking a look at the clang AST, all of these are properly preserved:

Grepping through the ast dump:

```sh
clang -std=gnu11 -Xclang -ast-dump -fsyntax-only test.c
```

The `const char *` is properly preserved, so I assume something is happening in between that's removing the const qualifier for the CIR side.

```
26:`-VarDecl 0x559e700dce58 line:3:7 p 'int (*)(const char *, const char *)' cinit
27: `-CStyleCastExpr 0x559e700dd088 'int (*)(const char *, const char *)'
28: `-ImplicitCastExpr 0x559e700dd070 'int (*)(const char *, const char *)' part_of_explicit_cast
29: `-DeclRefExpr 0x559e700dd008 'int (const char *, const char *)' Function 0x559e700dcb38 'strcmp' 'int (const char *, const char *)'
```

Contributor guide

Open the contributing guide

Research direction

Start with the test.c reproducer and run the provided clang command, then compare its CIR output with the AST dump from the second command. Trace the CIR lowering between the preserved C function types and the emitted cir.func and cir.global types; done means both retain const on the pointee types.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.