Skip to content

Commit

Permalink
AArch64: add __builtin_arm_trap
Browse files Browse the repository at this point in the history
It's useful to provide an indicator code with the trap, which the generic
__builtin_trap can't do. asm("brk #N") is an option, but following that with a
__builtin_unreachable() leads to two traps when the compiler doesn't know the
block can't return. So compiler support like this is useful.
  • Loading branch information
TNorthover committed Mar 14, 2024
1 parent 763be01 commit 4299c72
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3443,6 +3443,21 @@ Query for this feature with ``__has_builtin(__builtin_debugtrap)``.
Query for this feature with ``__has_builtin(__builtin_trap)``.
``__builtin_arm_trap``
------------------
``__builtin_arm_trap`` is an AArch64 extension to ``__builtin_trap`` which also accepts a compile-time constant value, encoded directly into the trap instruction for later inspection.
**Syntax**:
.. code-block:: c++
__builtin_arm_trap(const unsigned short payload)
**Description**
``__builtin_arm_trap`` is lowered to the ``llvm.aarch64.break`` builtin, and then to ``brk #payload``.
``__builtin_nondeterministic_value``
------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/BuiltinsAArch64.def
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ BUILTIN(__builtin_arm_wfi, "v", "")
BUILTIN(__builtin_arm_sev, "v", "")
BUILTIN(__builtin_arm_sevl, "v", "")

// Like __builtin_trap but provide an 16-bit immediate reason code (which goes into `brk #N`).
BUILTIN(__builtin_arm_trap, "vUIs", "nr")

// CRC32
TARGET_BUILTIN(__builtin_arm_crc32b, "UiUiUc", "nc", "crc")
TARGET_BUILTIN(__builtin_arm_crc32cb, "UiUiUc", "nc", "crc")
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10686,6 +10686,12 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
return Builder.CreateCall(F, llvm::ConstantInt::get(Int32Ty, HintID));
}

if (BuiltinID == clang::AArch64::BI__builtin_arm_trap) {
Function *F = CGM.getIntrinsic(Intrinsic::aarch64_break);
llvm::Value *Arg = EmitScalarExpr(E->getArg(0));
return Builder.CreateCall(F, Builder.CreateZExt(Arg, CGM.Int32Ty));
}

if (BuiltinID == clang::AArch64::BI__builtin_arm_get_sme_state) {
// Create call to __arm_sme_state and store the results to the two pointers.
CallInst *CI = EmitRuntimeCall(CGM.CreateRuntimeFunction(
Expand Down
6 changes: 6 additions & 0 deletions clang/test/CodeGen/builtins-arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,10 @@ int rndrrs(uint64_t *__addr) {
return __builtin_arm_rndrrs(__addr);
}

// CHECK-LABEL: @trap(
// CHECK: call void @llvm.aarch64.break(i32 42)
void trap() {
__builtin_arm_trap(42);
}

// CHECK: ![[M0]] = !{!"1:2:3:4:5"}
9 changes: 9 additions & 0 deletions clang/test/Sema/builtins-arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ void test_prefetch(void) {
__builtin_arm_prefetch(0, 0, 0, 2, 0); // expected-error-re {{argument value {{.*}} is outside the valid range}}
__builtin_arm_prefetch(0, 0, 0, 0, 2); // expected-error-re {{argument value {{.*}} is outside the valid range}}
}

void test_trap(short s, unsigned short us) {
__builtin_arm_trap(42);
__builtin_arm_trap(65535);
__builtin_arm_trap(-1);
__builtin_arm_trap(65536); // expected-warning {{implicit conversion from 'int' to 'unsigned short' changes value from 65536 to 0}}
__builtin_arm_trap(s); // expected-error {{argument to '__builtin_arm_trap' must be a constant integer}}
__builtin_arm_trap(us); // expected-error {{argument to '__builtin_arm_trap' must be a constant integer}}
}

0 comments on commit 4299c72

Please sign in to comment.