Skip to content

Commit ed95a08

Browse files
committed
[MS] Implement __iso_volatile loads/stores as builtins
These are supposed to produce the same as normal volatile pointer loads/stores. When -volatile:ms is specified, normal volatile pointers are forced to have atomic semantics (as is the default on x86 in MSVC mode). In that case, these builtins should still produce non-atomic volatile loads/stores without acquire/release semantics, which the new test verifies. These are only available on ARM (and on AArch64, although clang doesn't support AArch64/Windows yet). This implements what is missing for PR30394, making it possible to compile C++ for ARM in MSVC mode with MSVC headers. Differential Revision: https://reviews.llvm.org/D24986 llvm-svn: 282900
1 parent c162194 commit ed95a08

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

clang/include/clang/Basic/BuiltinsARM.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ LANGBUILTIN(__sevl, "v", "", ALL_MS_LANGUAGES)
115115
LANGBUILTIN(__dmb, "vUi", "nc", ALL_MS_LANGUAGES)
116116
LANGBUILTIN(__dsb, "vUi", "nc", ALL_MS_LANGUAGES)
117117
LANGBUILTIN(__isb, "vUi", "nc", ALL_MS_LANGUAGES)
118+
LANGBUILTIN(__iso_volatile_load8, "ccCD*", "n", ALL_MS_LANGUAGES)
119+
LANGBUILTIN(__iso_volatile_load16, "ssCD*", "n", ALL_MS_LANGUAGES)
120+
LANGBUILTIN(__iso_volatile_load32, "iiCD*", "n", ALL_MS_LANGUAGES)
121+
LANGBUILTIN(__iso_volatile_load64, "LLiLLiCD*", "n", ALL_MS_LANGUAGES)
122+
LANGBUILTIN(__iso_volatile_store8, "vcD*c", "n", ALL_MS_LANGUAGES)
123+
LANGBUILTIN(__iso_volatile_store16, "vsD*s", "n", ALL_MS_LANGUAGES)
124+
LANGBUILTIN(__iso_volatile_store32, "viD*i", "n", ALL_MS_LANGUAGES)
125+
LANGBUILTIN(__iso_volatile_store64, "vLLiD*LLi", "n", ALL_MS_LANGUAGES)
118126
LANGBUILTIN(__ldrexd, "WiWiCD*", "", ALL_MS_LANGUAGES)
119127
LANGBUILTIN(_MoveFromCoprocessor, "UiIUiIUiIUiIUiIUi", "", ALL_MS_LANGUAGES)
120128
LANGBUILTIN(_MoveFromCoprocessor2, "UiIUiIUiIUiIUiIUi", "", ALL_MS_LANGUAGES)

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4279,6 +4279,41 @@ Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,
42794279
return Builder.CreateCall(F, {StoreVal, StoreAddr}, "strex");
42804280
}
42814281

4282+
switch (BuiltinID) {
4283+
case ARM::BI__iso_volatile_load8:
4284+
case ARM::BI__iso_volatile_load16:
4285+
case ARM::BI__iso_volatile_load32:
4286+
case ARM::BI__iso_volatile_load64: {
4287+
Value *Ptr = EmitScalarExpr(E->getArg(0));
4288+
QualType ElTy = E->getArg(0)->getType()->getPointeeType();
4289+
CharUnits LoadSize = getContext().getTypeSizeInChars(ElTy);
4290+
llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
4291+
LoadSize.getQuantity() * 8);
4292+
Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
4293+
llvm::LoadInst *Load =
4294+
Builder.CreateAlignedLoad(Ptr, LoadSize);
4295+
Load->setVolatile(true);
4296+
return Load;
4297+
}
4298+
case ARM::BI__iso_volatile_store8:
4299+
case ARM::BI__iso_volatile_store16:
4300+
case ARM::BI__iso_volatile_store32:
4301+
case ARM::BI__iso_volatile_store64: {
4302+
Value *Ptr = EmitScalarExpr(E->getArg(0));
4303+
Value *Value = EmitScalarExpr(E->getArg(1));
4304+
QualType ElTy = E->getArg(0)->getType()->getPointeeType();
4305+
CharUnits StoreSize = getContext().getTypeSizeInChars(ElTy);
4306+
llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
4307+
StoreSize.getQuantity() * 8);
4308+
Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
4309+
llvm::StoreInst *Store =
4310+
Builder.CreateAlignedStore(Value, Ptr,
4311+
StoreSize);
4312+
Store->setVolatile(true);
4313+
return Store;
4314+
}
4315+
}
4316+
42824317
if (BuiltinID == ARM::BI__builtin_arm_clrex) {
42834318
Function *F = CGM.getIntrinsic(Intrinsic::arm_clrex);
42844319
return Builder.CreateCall(F);

clang/test/CodeGen/ms-volatile-arm.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// REQUIRES: arm-registered-target
2+
// RUN: %clang_cc1 -triple thumbv7-win32 -emit-llvm -fms-extensions -fms-volatile -o - < %s | FileCheck %s
3+
4+
void test1(int volatile *p, int v) {
5+
__iso_volatile_store32(p, v);
6+
// CHECK-LABEL: @test1
7+
// CHECK: store volatile {{.*}}, {{.*}}
8+
}
9+
int test2(const int volatile *p) {
10+
return __iso_volatile_load32(p);
11+
// CHECK-LABEL: @test2
12+
// CHECK: load volatile {{.*}}
13+
}

0 commit comments

Comments
 (0)