[clang] [clang][LoongArch] Check target features in CheckLoongArchBuiltinFunctionCall (PR #191811)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 05:17:10 PDT 2026


https://github.com/lrzlin updated https://github.com/llvm/llvm-project/pull/191811

>From 643fea068df08110961a5708359acba2ce4e35dc Mon Sep 17 00:00:00 2001
From: Lin Runze <linrunze at loongson.cn>
Date: Tue, 14 Apr 2026 16:50:28 +0800
Subject: [PATCH] [clang][LoongArch] Check target features in
 CheckLoongArchBuiltinFunctionCall

---
 clang/lib/Sema/SemaLoongArch.cpp                 | 16 ++++++++++++++++
 .../builtin-loongarch-check-target-feature.cpp   |  5 +++++
 2 files changed, 21 insertions(+)
 create mode 100644 clang/test/Sema/builtin-loongarch-check-target-feature.cpp

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