[clang] e3db5e0 - [clang][LoongArch] Check target features in CheckLoongArchBuiltinFunctionCall (#191811)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 19 19:33:14 PDT 2026
Author: lrzlin
Date: 2026-04-20T10:33:09+08:00
New Revision: e3db5e0cb17199bcb30fba51ff6cf58094fa1f7c
URL: https://github.com/llvm/llvm-project/commit/e3db5e0cb17199bcb30fba51ff6cf58094fa1f7c
DIFF: https://github.com/llvm/llvm-project/commit/e3db5e0cb17199bcb30fba51ff6cf58094fa1f7c.diff
LOG: [clang][LoongArch] Check target features in CheckLoongArchBuiltinFunctionCall (#191811)
Add target features check in `CheckLoongArchBuiltinFunctionCall`, thus
we could through an error
when pass the `-mno-lsx` to clang while using the builtin LSX intrinsics
for global variables instead of
trigger an ICE.
Minimal Example:
```
// clang-20 --march=loongarch64 -mno-lsx -S -o - "x.cc"
__attribute__((__vector_size__(16))) long foo = __builtin_lsx_vinsgr2vr_w(foo, 0, 0);
```
and the compiler will output
```
x.cc:1:49: error: builtin needs target feature lsx
1 | __attribute__((__vector_size__(16))) long foo = __builtin_lsx_vinsgr2vr_w(foo, 0, 0);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
```
Fix https://github.com/llvm/llvm-project/issues/131771
Added:
clang/test/Sema/builtin-loongarch-check-target-feature.cpp
Modified:
clang/lib/Sema/SemaLoongArch.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaLoongArch.cpp b/clang/lib/Sema/SemaLoongArch.cpp
index c57a6f91a3dfc..bf09b300dc52c 100644
--- a/clang/lib/Sema/SemaLoongArch.cpp
+++ b/clang/lib/Sema/SemaLoongArch.cpp
@@ -11,8 +11,11 @@
//===----------------------------------------------------------------------===//
#include "clang/Sema/SemaLoongArch.h"
+#include "clang/Basic/DiagnosticFrontend.h"
#include "clang/Basic/TargetBuiltins.h"
+#include "clang/Basic/TargetInfo.h"
#include "clang/Sema/Sema.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MathExtras.h"
namespace clang {
@@ -22,6 +25,19 @@ SemaLoongArch::SemaLoongArch(Sema &S) : SemaBase(S) {}
bool SemaLoongArch::CheckLoongArchBuiltinFunctionCall(const TargetInfo &TI,
unsigned BuiltinID,
CallExpr *TheCall) {
+ ASTContext &Context = getASTContext();
+ const FunctionDecl *FD = SemaRef.getCurFunctionDecl();
+ llvm::StringMap<bool> FeatureMap;
+ Context.getFunctionFeatureMap(FeatureMap, FD);
+
+ llvm::StringRef Features = Context.BuiltinInfo.getRequiredFeatures(BuiltinID);
+ // Only check it when the builtin is not used in a function.
+ if (!Features.empty() && !FD) {
+ if (!Builtin::evaluateRequiredTargetFeatures(Features, FeatureMap))
+ return Diag(TheCall->getBeginLoc(), diag::err_builtin_needs_feature)
+ << "builtin" << Features;
+ }
+
switch (BuiltinID) {
default:
break;
diff --git a/clang/test/Sema/builtin-loongarch-check-target-feature.cpp b/clang/test/Sema/builtin-loongarch-check-target-feature.cpp
new file mode 100644
index 0000000000000..d122ac8408c5a
--- /dev/null
+++ b/clang/test/Sema/builtin-loongarch-check-target-feature.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -triple loongarch64 -fsyntax-only -verify %s
+typedef long long __m128i __attribute__ ((__vector_size__ (16), __may_alias__));
+
+__m128i foo = __builtin_lsx_vinsgr2vr_w({0, 0}, 0, 0); // expected-error {{builtin needs target feature lsx}}
+__m128i bar = __builtin_lsx_vfrsqrte_s({0, 0}); // expected-error {{builtin needs target feature lsx,frecipe}}
\ No newline at end of file
More information about the cfe-commits
mailing list