Skip to content

Revert "[clang][Sema] Declare builtins used in #pragma intrinsic" #141994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025

Conversation

sarnex
Copy link
Member

@sarnex sarnex commented May 29, 2025

Reverts #138205

Breaks Chrome.

@sarnex sarnex marked this pull request as ready for review May 29, 2025 23:42
@sarnex sarnex merged commit cda1853 into main May 29, 2025
14 checks passed
@sarnex sarnex deleted the revert-138205-syswarn branch May 29, 2025 23:42
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 29, 2025
@llvmbot
Copy link
Member

llvmbot commented May 29, 2025

@llvm/pr-subscribers-clang

Author: Nick Sarnie (sarnex)

Changes

Reverts llvm/llvm-project#138205

Breaks Chrome.


Full diff: https://github.com/llvm/llvm-project/pull/141994.diff

3 Files Affected:

  • (modified) clang/lib/Parse/ParsePragma.cpp (+3-15)
  • (removed) clang/test/Sema/Inputs/builtin-system-header.h (-9)
  • (removed) clang/test/Sema/builtin-pragma-intrinsic.c (-25)
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index 4e67fd033b9aa..77b61af768993 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -301,13 +301,9 @@ struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
 };
 
 struct PragmaMSIntrinsicHandler : public PragmaHandler {
-  PragmaMSIntrinsicHandler(Sema &Actions)
-      : PragmaHandler("intrinsic"), Actions(Actions) {}
+  PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
                     Token &FirstToken) override;
-
-private:
-  Sema &Actions;
 };
 
 // "\#pragma fenv_access (on)".
@@ -521,7 +517,7 @@ void Parser::initializePragmaHandlers() {
     PP.AddPragmaHandler(MSOptimize.get());
     MSRuntimeChecks = std::make_unique<PragmaMSRuntimeChecksHandler>();
     PP.AddPragmaHandler(MSRuntimeChecks.get());
-    MSIntrinsic = std::make_unique<PragmaMSIntrinsicHandler>(Actions);
+    MSIntrinsic = std::make_unique<PragmaMSIntrinsicHandler>();
     PP.AddPragmaHandler(MSIntrinsic.get());
     MSFenvAccess = std::make_unique<PragmaMSFenvAccessHandler>();
     PP.AddPragmaHandler(MSFenvAccess.get());
@@ -3797,15 +3793,7 @@ void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
     if (!II->getBuiltinID())
       PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
           << II << SuggestIntrinH;
-    // If the builtin hasn't already been declared, declare it now.
-    DeclarationNameInfo NameInfo(II, Tok.getLocation());
-    LookupResult Previous(Actions, NameInfo, Sema::LookupOrdinaryName,
-                          Actions.forRedeclarationInCurContext());
-    Actions.LookupName(Previous, Actions.getCurScope(),
-                       /*CreateBuiltins*/ false);
-    if (Previous.empty())
-      Actions.LazilyCreateBuiltin(II, II->getBuiltinID(), Actions.getCurScope(),
-                                  /*ForRedeclaration*/ true, Tok.getLocation());
+
     PP.Lex(Tok);
     if (Tok.isNot(tok::comma))
       break;
diff --git a/clang/test/Sema/Inputs/builtin-system-header.h b/clang/test/Sema/Inputs/builtin-system-header.h
deleted file mode 100644
index 7eeb8d811fcfa..0000000000000
--- a/clang/test/Sema/Inputs/builtin-system-header.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifdef USE_PRAGMA_BEFORE
-#pragma intrinsic(_InterlockedOr64)
-#endif
-
-#define MACRO(x,y) _InterlockedOr64(x,y);
-
-#ifdef USE_PRAGMA_AFTER
-#pragma intrinsic(_InterlockedOr64)
-#endif
diff --git a/clang/test/Sema/builtin-pragma-intrinsic.c b/clang/test/Sema/builtin-pragma-intrinsic.c
deleted file mode 100644
index 1e8507bfd37df..0000000000000
--- a/clang/test/Sema/builtin-pragma-intrinsic.c
+++ /dev/null
@@ -1,25 +0,0 @@
-// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_BEFORE
-// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_AFTER
-// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_AFTER_USE
-// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_SAME_FILE
-// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s
-
-#if defined(USE_PRAGMA_BEFORE) || defined(USE_PRAGMA_AFTER) || defined(USE_PRAGMA_SAME_FILE)
-// expected-no-diagnostics
-#else
-// expected-error@+10 {{call to undeclared library function '_InterlockedOr64'}}
-// expected-note@+9 {{include the header <intrin.h> or explicitly provide a declaration for '_InterlockedOr64'}}
-#endif
-#include <builtin-system-header.h>
-
-#ifdef USE_PRAGMA_SAME_FILE
-#pragma intrinsic(_InterlockedOr64)
-#endif
-
-void foo() {
-  MACRO(0,0);
-}
-
-#ifdef USE_PRAGMA_AFTER_USE
-#pragma intrinsic(_InterlockedOr64)
-#endif

@alanzhao1
Copy link
Contributor

Thanks!

@sarnex
Copy link
Member Author

sarnex commented May 29, 2025

Sorry for the trouble

sarnex added a commit to sarnex/llvm-project that referenced this pull request May 30, 2025
sarnex added a commit to sarnex/llvm-project that referenced this pull request May 30, 2025
sarnex added a commit to sarnex/llvm-project that referenced this pull request May 30, 2025
sarnex added a commit that referenced this pull request Jun 3, 2025
…insic #138205' (#142019)

I had to revert #138205 in
#141994 because it broke the
Chrome build.

The problem came down to the following:

```c++
unsigned __int64 _umul128(unsigned __int64, unsigned __int64,
                          unsigned __int64 *);

namespace {}
#pragma intrinsic(_umul128)

 void foo() {
   unsigned __int64 carry;
   unsigned __int64 low = _umul128(0, 0, &carry);
 }
```

When processing the `#pragma intrinsic` line, we do a name lookup to see
if the builtin was previously declared. In this case the lookup fails
because the current namespace of the parser and sema is the above
namespace scope. The processing of the pragma happens as part of the
namespace close parsing. This is usually fine because most pragmas don't
care about scopes. However, that's not true for this and other MS
pragmas.

To fix this, we change the `#pragma intrinsic` processing to be the same
as other MS pragmas such as "optimize". Those are processed like a
declaration, and because of that we have the correct current scope, so
the lookup succeeds.

I added a test case that locks down the Chrome fix, as well as manually
tested the Chrome build and confirmed it passed.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 3, 2025
…ragma intrinsic #138205' (#142019)

I had to revert llvm/llvm-project#138205 in
llvm/llvm-project#141994 because it broke the
Chrome build.

The problem came down to the following:

```c++
unsigned __int64 _umul128(unsigned __int64, unsigned __int64,
                          unsigned __int64 *);

namespace {}
#pragma intrinsic(_umul128)

 void foo() {
   unsigned __int64 carry;
   unsigned __int64 low = _umul128(0, 0, &carry);
 }
```

When processing the `#pragma intrinsic` line, we do a name lookup to see
if the builtin was previously declared. In this case the lookup fails
because the current namespace of the parser and sema is the above
namespace scope. The processing of the pragma happens as part of the
namespace close parsing. This is usually fine because most pragmas don't
care about scopes. However, that's not true for this and other MS
pragmas.

To fix this, we change the `#pragma intrinsic` processing to be the same
as other MS pragmas such as "optimize". Those are processed like a
declaration, and because of that we have the correct current scope, so
the lookup succeeds.

I added a test case that locks down the Chrome fix, as well as manually
tested the Chrome build and confirmed it passed.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants