intel / intel/llvm

Better handling of `static` variables by `cgeist`.

Open
#8,076 1 comment 0 reactions 0 assignees View on GitHub
enhancement sycl-mlir
Dominant language
LLVM
Stars
1.5k
Forks
854
Avg merge
3d 17h
Merged PRs (30d)
137

Description

**Is your feature request related to a problem? Please describe**

`cgeist` currently generates unnecessary code for `static` variables:

```cpp
int get() {
static int x = 7;
return x;
}
```

compiles to:

```mlir
func.func @get_local_static() -> i32 attributes {llvm.linkage = #llvm.linkage} {
%false = arith.constant false
%c7_i32 = arith.constant 7 : i32
%0 = memref.get_global @"get_local_static@static@x" : memref
%alloca = memref.alloca() : memref<1xindex>
%reshape = memref.reshape %0(%alloca) : (memref, memref<1xindex>) -> memref<1xi32>
%1 = memref.get_global @"get_local_static@static@x@init" : memref
%alloca_0 = memref.alloca() : memref<1xindex>
%reshape_1 = memref.reshape %1(%alloca_0) : (memref, memref<1xindex>) -> memref<1xi1>
%2 = affine.load %reshape_1[0] : memref<1xi1>
scf.if %2 {
affine.store %false, %reshape_1[0] : memref<1xi1>
affine.store %c7_i32, %reshape[0] : memref<1xi32>
}
%3 = affine.load %reshape[0] : memref<1xi32>
return %3 : i32
}
```

1. We would not need a reshape if the variable were declared `memref<1xi32>`.
2. That if statement is not needed, as the variable is statically initialized.

**Describe the solution you would like**

Change type of variables to `memref<1xty>` and do not try to lazily initialize already initialized values, resulting in:

```mlir
func.func @get_local_static() -> i32 attributes {llvm.linkage = #llvm.linkage} {
%0 = memref.get_global @"get_local_static@static@x" : memref<1xi32>
%1 = affine.load %0[0] : memref<1xi32>
return %1 : i32
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.