llvm-project/llvm/test/Transforms/GlobalOpt/malloc-promote-atomic.ll
Nikita Popov 57530c23a5
[GlobalOpt] Do not promote malloc if there are atomic loads/stores (#137158)
When converting a malloc stored to a global into a global, we will
introduce an i1 flag to track whether the global has been initialized.

In case of atomic loads/stores, this will result in verifier failures,
because atomic ops on i1 are illegal. Even if we changed this to i8, I
don't think it is a good idea to change atomic types in that way.

Instead, bail out of the transform is we encounter any atomic
loads/stores of the global.

Fixes https://github.com/llvm/llvm-project/issues/137152.
2025-04-24 15:15:47 +02:00

29 lines
932 B
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes=globalopt -S < %s | FileCheck %s
@g = internal global ptr null, align 8
define void @init() {
; CHECK-LABEL: define void @init() local_unnamed_addr {
; CHECK-NEXT: [[ALLOC:%.*]] = call ptr @malloc(i64 48)
; CHECK-NEXT: store atomic ptr [[ALLOC]], ptr @g seq_cst, align 8
; CHECK-NEXT: ret void
;
%alloc = call ptr @malloc(i64 48)
store atomic ptr %alloc, ptr @g seq_cst, align 8
ret void
}
define i1 @check() {
; CHECK-LABEL: define i1 @check() local_unnamed_addr {
; CHECK-NEXT: [[VAL:%.*]] = load atomic ptr, ptr @g seq_cst, align 8
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[VAL]], null
; CHECK-NEXT: ret i1 [[CMP]]
;
%val = load atomic ptr, ptr @g seq_cst, align 8
%cmp = icmp eq ptr %val, null
ret i1 %cmp
}
declare ptr @malloc(i64) allockind("alloc,uninitialized") allocsize(0)