llvm-project/clang/test/CodeGen/ms-declspecs.c
Nikita Popov 14cc7a0772 [Clang] Allow __declspec(noalias) to access inaccessible memory
MSVC defines __declspec(noalias) as follows (https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/k649tyc7(v=vs.110)?redirectedfrom=MSDN):

> noalias means that a function call does not modify or reference
> visible global state and only modifies the memory pointed to
> directly by pointer parameters (first-level indirections).

> If a function is annotated as noalias, the optimizer can assume
> that, in addition to the parameters themselves, only first-level
> indirections of pointer parameters are referenced or modified
> inside the function. The visible global state is the set of all
> data that is not defined or referenced outside of the compilation
> scope, and their address is not taken. The compilation scope is
> all source files (/LTCG (Link-time Code Generation) builds) or a
> single source file (non-/LTCG build).

The wording is not super clear to me, but I believe this is saying
that __declspec(noalias) functions may access inaccessible memory
(i.e. non-visible global state in their words). Indeed, the Windows
CRT applies this attribute to malloc, which does access inaccessible
memory under LLVM's memory model.

As such, change the attribute to emit
memory(argmem: readwrite, inaccessiblemem: readwrite) instead of
memory(argmem: readwrite).

Fixes https://github.com/llvm/llvm-project/issues/64827.

Differential Revision: https://reviews.llvm.org/D158984
2023-08-29 11:43:57 +02:00

45 lines
1.6 KiB
C

// RUN: %clang_cc1 -triple i386-pc-win32 %s -emit-llvm -fms-compatibility -O2 -disable-llvm-passes -o - | FileCheck %s
__declspec(selectany) int x1 = 1;
const __declspec(selectany) int x2 = 2;
// CHECK: @x1 = weak_odr dso_local global i32 1, comdat, align 4
// CHECK: @x2 = weak_odr dso_local constant i32 2, comdat, align 4
// selectany turns extern variable declarations into definitions.
__declspec(selectany) int x3;
extern __declspec(selectany) int x4;
// CHECK: @x3 = weak_odr dso_local global i32 0, comdat, align 4
// CHECK: @x4 = weak_odr dso_local global i32 0, comdat, align 4
struct __declspec(align(16)) S {
char x;
};
union { struct S s; } u;
// CHECK: @u = {{.*}}zeroinitializer, align 16
// CHECK: define dso_local void @t3() [[NAKED:#[0-9]+]] {
__declspec(naked) void t3(void) {}
// CHECK: define dso_local void @t22() [[NUW:#[0-9]+]]
void __declspec(nothrow) t22(void);
void t22(void) {}
// CHECK: define dso_local void @t2() [[NI:#[0-9]+]] {
__declspec(noinline) void t2(void) {}
// CHECK: call void @f20_t() [[NR:#[0-9]+]]
__declspec(noreturn) void f20_t(void);
void f20(void) { f20_t(); }
__declspec(noalias) void noalias_callee(int *x);
// CHECK: call void @noalias_callee({{.*}}) [[NA:#[0-9]+]]
void noalias_caller(int *x) { noalias_callee(x); }
// CHECK: attributes [[NAKED]] = { naked noinline nounwind{{.*}} }
// CHECK: attributes [[NUW]] = { nounwind{{.*}} }
// CHECK: attributes [[NI]] = { noinline nounwind{{.*}} }
// CHECK: attributes [[NR]] = { noreturn }
// CHECK: attributes [[NA]] = { nounwind memory(argmem: readwrite, inaccessiblemem: readwrite){{.*}} }