llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.32k stars 11.69k forks source link

[CLANG] Optimization level changes program output #89543

Open maxomatic458 opened 5 months ago

maxomatic458 commented 5 months ago

this program in IR should exit with code 80

define i64* @int_Index_int_int(i64* %_self,i64 %_idx)  {
entry:
%_idx_0 = alloca i64
store i64 %_idx, i64* %_idx_0
%_4 = load i64, i64* %_self
%_5 = load i64, i64* %_idx_0
%_6 = mul i64 %_4, %_5
%_3 = alloca i64
store i64 %_6, i64* %_3
%_2 = alloca i64*
store i64* %_3, i64** %_2
%_1 = load i64*, i64** %_2
ret i64* %_1
}
define i64 @main()  {
entry:
%_9 = alloca i64
store i64 10, i64* %_9
%_8 = load i64, i64* %_9
%_a_7 = alloca i64
store i64 %_8, i64* %_a_7
%_12 = alloca i64
store i64 20, i64* %_12
%_11 = load i64, i64* %_12
%_b_10 = alloca i64
store i64 %_11, i64* %_b_10
%_17 = alloca i64
store i64 2, i64* %_17
%_16 = load i64, i64* %_17
%_22 = alloca i64
store i64 2, i64* %_22
%_21 = load i64, i64* %_22
%_19 = call i64* @int_Index_int_int(i64* %_a_7,i64 %_21)
%_14 = load i64, i64* %_19
%_c_13 = alloca i64
store i64 %_14, i64* %_c_13
%_27 = alloca i64
store i64 3, i64* %_27
%_26 = load i64, i64* %_27
%_32 = alloca i64
store i64 3, i64* %_32
%_31 = load i64, i64* %_32
%_29 = call i64* @int_Index_int_int(i64* %_b_10,i64 %_31)
%_24 = load i64, i64* %_29
%_d_23 = alloca i64
store i64 %_24, i64* %_d_23
%_34 = load i64, i64* %_c_13
%_35 = load i64, i64* %_d_23
%_36 = add i64 %_34, %_35
ret i64 %_36
}

but it will only exit with 80 when the opt level is 1 or 0

the exit code for -O2 and -O3 will change every time the IR is compiled

bjope commented 5 months ago

Looks like bad IR to me. Afaict int_Index_int_int returns a pointer to a local alloca. That pointer is dangling upon the return. See semantics for alloca here: https://llvm.org/docs/LangRef.html#alloca-instruction