[clang] f382545 - [clang-cl] Handle some pragma alloc_text corner cases handled by MSVC

2022-06-29 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-06-29T06:45:59-07:00
New Revision: f382545b2ba8a39435f7efa02dadc722c429d2cd

URL: 
https://github.com/llvm/llvm-project/commit/f382545b2ba8a39435f7efa02dadc722c429d2cd
DIFF: 
https://github.com/llvm/llvm-project/commit/f382545b2ba8a39435f7efa02dadc722c429d2cd.diff

LOG: [clang-cl] Handle some pragma alloc_text corner cases handled by MSVC

MSVC's pragma alloc_text accepts a function that was redeclared in
a non extern-C context if the previous declaration was in an extern-C
context. i.e.

```
extern "C" { static void f(); }
static void f();
```

MSVC's pragma alloc_text also rejects non-functions.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D128649

Added: 


Modified: 
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaAttr.cpp
clang/test/Sema/pragma-ms-alloc-text.c
clang/test/Sema/pragma-ms-alloc-text.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 442088d078d98..dc9ca4bd18e2e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -992,6 +992,8 @@ def err_pragma_expected_file_scope : Error<
   "'#pragma %0' can only appear at file scope">;
 def err_pragma_alloc_text_c_linkage: Error<
   "'#pragma alloc_text' is applicable only to functions with C linkage">;
+def err_pragma_alloc_text_not_function: Error<
+  "'#pragma alloc_text' is applicable only to functions">;
 
 def warn_pragma_unused_undeclared_var : Warning<
   "undeclared variable %0 used as an argument for '#pragma unused'">,

diff  --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index c7e62e5895533..c997d018a4065 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -810,8 +810,13 @@ void Sema::ActOnPragmaMSAllocText(
   return;
 }
 
-DeclContext *DC = ND->getDeclContext();
-if (getLangOpts().CPlusPlus && !DC->isExternCContext()) {
+auto *FD = dyn_cast(ND->getCanonicalDecl());
+if (!FD) {
+  Diag(Loc, diag::err_pragma_alloc_text_not_function);
+  return;
+}
+
+if (getLangOpts().CPlusPlus && !FD->isInExternCContext()) {
   Diag(Loc, diag::err_pragma_alloc_text_c_linkage);
   return;
 }

diff  --git a/clang/test/Sema/pragma-ms-alloc-text.c 
b/clang/test/Sema/pragma-ms-alloc-text.c
index a5f2e9f11dce2..ff49fa475cd96 100644
--- a/clang/test/Sema/pragma-ms-alloc-text.c
+++ b/clang/test/Sema/pragma-ms-alloc-text.c
@@ -1,9 +1,12 @@
 // RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify %s
 
 void foo();
-#pragma alloc_text("hello", foo) // expected-no-diagnostics
+#pragma alloc_text("hello", foo) // no-error
 void foo() {}
 
 static void foo1();
-#pragma alloc_text("hello", foo1) // expected-no-diagnostics
+#pragma alloc_text("hello", foo1) // no-error
 void foo1() {}
+
+int foo2;
+#pragma alloc_text(c, foo2) // expected-error {{'#pragma alloc_text' is 
applicable only to functions}}

diff  --git a/clang/test/Sema/pragma-ms-alloc-text.cpp 
b/clang/test/Sema/pragma-ms-alloc-text.cpp
index 931b152fe78cc..a150cb2d815af 100644
--- a/clang/test/Sema/pragma-ms-alloc-text.cpp
+++ b/clang/test/Sema/pragma-ms-alloc-text.cpp
@@ -40,3 +40,20 @@ static void foo6();
 #pragma alloc_text(c, foo6) // no-warning
 void foo6() {}
 }
+
+extern "C" {
+static void foo7();
+}
+static void foo7();
+#pragma alloc_text(c, foo7) // no-warning
+void foo7() {}
+
+static void foo8();
+extern "C" {
+static void foo8();
+}
+#pragma alloc_text(c, foo8) // expected-error {{'#pragma alloc_text' is 
applicable only to functions with C linkage}}
+void foo8() {}
+
+enum foo9 { A, B, C };
+#pragma alloc_text(c, foo9) // expected-error {{'#pragma alloc_text' is 
applicable only to functions}}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 7f9837c - [Headers][MSVC] Define wchar_t in stddef.h like MSVC if not using the builtin type

2022-05-12 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-05-12T09:38:07-07:00
New Revision: 7f9837cfa63663ccd51da3e5de73acec8f776ee8

URL: 
https://github.com/llvm/llvm-project/commit/7f9837cfa63663ccd51da3e5de73acec8f776ee8
DIFF: 
https://github.com/llvm/llvm-project/commit/7f9837cfa63663ccd51da3e5de73acec8f776ee8.diff

LOG: [Headers][MSVC] Define wchar_t in stddef.h like MSVC if not using the 
builtin type

 MSVC expects wchar_t to be defined in stddef.h if /Zc:wchar_t- is specified

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D124026

Added: 
clang/test/Headers/ms-no-wchar.cpp

Modified: 
clang/lib/Headers/stddef.h

Removed: 




diff  --git a/clang/lib/Headers/stddef.h b/clang/lib/Headers/stddef.h
index 15acd4427ca14..a15d21b553174 100644
--- a/clang/lib/Headers/stddef.h
+++ b/clang/lib/Headers/stddef.h
@@ -62,7 +62,7 @@ typedef __SIZE_TYPE__ rsize_t;
 #endif /* defined(__need_STDDEF_H_misc) */
 
 #if defined(__need_wchar_t)
-#ifndef __cplusplus
+#if !defined(__cplusplus) || (defined(_MSC_VER) && !_NATIVE_WCHAR_T_DEFINED)
 /* Always define wchar_t when modules are available. */
 #if !defined(_WCHAR_T) || __has_feature(modules)
 #if !__has_feature(modules)

diff  --git a/clang/test/Headers/ms-no-wchar.cpp 
b/clang/test/Headers/ms-no-wchar.cpp
new file mode 100644
index 0..8fe1dbc4c87f6
--- /dev/null
+++ b/clang/test/Headers/ms-no-wchar.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -triple x86_64-pc-windows-msvc 
-fms-compatibility-version=17.00 -fno-wchar %s
+// MSVC defines wchar_t instead of using the builtin if /Zc:wchar_t- is passed
+
+#include 
+
+wchar_t c;



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 3946de0 - [MSVC] Add support for pragma function

2022-05-13 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-05-13T06:39:47-07:00
New Revision: 3946de0456fcbcd3a6c9f0d09d16d46c7987e0d7

URL: 
https://github.com/llvm/llvm-project/commit/3946de0456fcbcd3a6c9f0d09d16d46c7987e0d7
DIFF: 
https://github.com/llvm/llvm-project/commit/3946de0456fcbcd3a6c9f0d09d16d46c7987e0d7.diff

LOG: [MSVC] Add support for pragma function

MSVC pragma function tells the compiler to generate calls to functions in the 
pragma function list, instead of using the builtin. Needs 
https://reviews.llvm.org/D124701

https://docs.microsoft.com/en-us/cpp/preprocessor/function-c-cpp?view=msvc-170

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D124702

Added: 
clang/test/CodeGen/pragma-ms-function.c

Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Parse/Parser.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParsePragma.cpp
clang/lib/Sema/SemaAttr.cpp
clang/lib/Sema/SemaDecl.cpp
clang/test/Preprocessor/pragma_microsoft.c
clang/test/Preprocessor/pragma_microsoft.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5b12400701c68..2a43880c2fd74 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -278,6 +278,9 @@ Removed Compiler Flags
 
 New Pragmas in Clang
 
+- Added support for MSVC's ``#pragma function``, which tells the compiler to
+  generate calls to functions listed in the pragma instead of using the
+  builtins.
 
 - ...
 

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d8f87747e58d0..dfca233033a9f 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -988,6 +988,9 @@ def err_invalid_super_scope : Error<"invalid use of 
'__super', "
 def err_super_in_lambda_unsupported : Error<
   "use of '__super' inside a lambda is unsupported">;
 
+def err_pragma_expected_file_scope : Error<
+  "'#pragma %0' can only appear at file scope">;
+
 def warn_pragma_unused_undeclared_var : Warning<
   "undeclared variable %0 used as an argument for '#pragma unused'">,
   InGroup;

diff  --git a/clang/include/clang/Parse/Parser.h 
b/clang/include/clang/Parse/Parser.h
index caa58d9260628..4e4fcb53474ec 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -196,6 +196,7 @@ class Parser : public CodeCompletionHandler {
   std::unique_ptr MSSection;
   std::unique_ptr MSRuntimeChecks;
   std::unique_ptr MSIntrinsic;
+  std::unique_ptr MSFunction;
   std::unique_ptr MSOptimize;
   std::unique_ptr MSFenvAccess;
   std::unique_ptr CUDAForceHostDeviceHandler;
@@ -722,6 +723,8 @@ class Parser : public CodeCompletionHandler {
  SourceLocation PragmaLocation);
   bool HandlePragmaMSInitSeg(StringRef PragmaName,
  SourceLocation PragmaLocation);
+  bool HandlePragmaMSFunction(StringRef PragmaName,
+  SourceLocation PragmaLocation);
 
   /// Handle the annotation token produced for
   /// #pragma align...

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 27603f0b891f3..13d403bf8db94 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -755,6 +755,9 @@ class Sema final {
   /// optimizations are currently "on", this is set to an invalid location.
   SourceLocation OptimizeOffPragmaLocation;
 
+  /// Set of no-builtin functions listed by \#pragma function.
+  llvm::SmallSetVector MSFunctionNoBuiltins;
+
   /// Flag indicating if Sema is building a recovery call expression.
   ///
   /// This flag is used to avoid building recovery call expressions
@@ -10322,6 +10325,11 @@ class Sema final {
   /// Called on well formed \#pragma clang optimize.
   void ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc);
 
+  /// Call on well formed \#pragma function.
+  void
+  ActOnPragmaMSFunction(SourceLocation Loc,
+const llvm::SmallVectorImpl &NoBuiltins);
+
   /// Get the location for the currently active "\#pragma clang optimize
   /// off". If this location is invalid, then the state of the pragma is "on".
   SourceLocation getOptimizeOffPragmaLocation() const {
@@ -10338,6 +10346,11 @@ class Sema final {
   /// attribute to be added (usually because of a pragma).
   void AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, SourceLocation Loc);
 
+  /// Only called on function definitions; if there is a pragma in scope
+  /// with the effect of a range-based no_builtin, consider marking the 
function
+  /// with attribute no_builtin.
+  void AddImplicitMSFunctionNoBuiltinAttr(FunctionDecl *FD);
+
   /// AddAlignedAttr - Adds an aligned attribute to a particular declaration.
   void AddAlignedAttr(Decl *D, 

[clang] b147717 - [MSVC] Add support for pragma alloc_text

2022-05-16 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-05-16T07:00:17-07:00
New Revision: b147717bb36c915bedfb33c07259cac4f09502a1

URL: 
https://github.com/llvm/llvm-project/commit/b147717bb36c915bedfb33c07259cac4f09502a1
DIFF: 
https://github.com/llvm/llvm-project/commit/b147717bb36c915bedfb33c07259cac4f09502a1.diff

LOG: [MSVC] Add support for pragma alloc_text

`#pragma alloc_text` is a MSVC pragma that names the code section where 
functions should be placed. It only
applies to functions with C linkage.

https://docs.microsoft.com/en-us/cpp/preprocessor/alloc-text?view=msvc-170

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D125011

Added: 
clang/test/CodeGen/msvc_pragma_alloc_text.cpp
clang/test/Sema/pragma-ms-alloc-text.cpp

Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Parse/Parser.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParsePragma.cpp
clang/lib/Sema/SemaAttr.cpp
clang/lib/Sema/SemaDecl.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2a43880c2fd74..59c42384c4ed7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -281,6 +281,9 @@ New Pragmas in Clang
 - Added support for MSVC's ``#pragma function``, which tells the compiler to
   generate calls to functions listed in the pragma instead of using the
   builtins.
+- Added support for MSVC's ``#pragma alloc_text``. The pragma names the code
+  section functions are placed in. The pragma only applies to functions with
+  C linkage.
 
 - ...
 

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index dfca233033a9f..6dcf2eafbf210 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -990,6 +990,8 @@ def err_super_in_lambda_unsupported : Error<
 
 def err_pragma_expected_file_scope : Error<
   "'#pragma %0' can only appear at file scope">;
+def err_pragma_alloc_text_c_linkage: Error<
+  "'#pragma alloc_text' is applicable only to functions with C linkage">;
 
 def warn_pragma_unused_undeclared_var : Warning<
   "undeclared variable %0 used as an argument for '#pragma unused'">,

diff  --git a/clang/include/clang/Parse/Parser.h 
b/clang/include/clang/Parse/Parser.h
index 4e4fcb53474ec..e44ef6bb8f19d 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -199,6 +199,7 @@ class Parser : public CodeCompletionHandler {
   std::unique_ptr MSFunction;
   std::unique_ptr MSOptimize;
   std::unique_ptr MSFenvAccess;
+  std::unique_ptr MSAllocText;
   std::unique_ptr CUDAForceHostDeviceHandler;
   std::unique_ptr OptimizeHandler;
   std::unique_ptr LoopHintHandler;
@@ -725,6 +726,8 @@ class Parser : public CodeCompletionHandler {
  SourceLocation PragmaLocation);
   bool HandlePragmaMSFunction(StringRef PragmaName,
   SourceLocation PragmaLocation);
+  bool HandlePragmaMSAllocText(StringRef PragmaName,
+   SourceLocation PragmaLocation);
 
   /// Handle the annotation token produced for
   /// #pragma align...

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 13d403bf8db94..e9bd7569984c9 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -724,6 +724,9 @@ class Sema final {
   StringLiteral *CurInitSeg;
   SourceLocation CurInitSegLoc;
 
+  /// Sections used with #pragma alloc_text.
+  llvm::StringMap> FunctionToSectionMap;
+
   /// VisContext - Manages the stack for \#pragma GCC visibility.
   void *VisContext; // Really a "PragmaVisStack*"
 
@@ -10200,6 +10203,12 @@ class Sema final {
   void ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
 StringLiteral *SegmentName);
 
+  /// Called on well-formed \#pragma alloc_text().
+  void ActOnPragmaMSAllocText(
+  SourceLocation PragmaLocation, StringRef Section,
+  const SmallVector>
+  &Functions);
+
   /// Called on #pragma clang __debug dump II
   void ActOnPragmaDump(Scope *S, SourceLocation Loc, IdentifierInfo *II);
 
@@ -10341,6 +10350,11 @@ class Sema final {
   /// with attribute optnone.
   void AddRangeBasedOptnone(FunctionDecl *FD);
 
+  /// Only called on function definitions; if there is a `#pragma alloc_text`
+  /// that decides which code section the function should be in, add
+  /// attribute section to the function.
+  void AddSectionMSAllocText(FunctionDecl *FD);
+
   /// Adds the 'optnone' attribute to the function declaration if there
   /// are no conflicts; Loc represents the location causing the 'optnone'
   /// attribute to be added (usually because of a pragma).

diff  --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index cf637fc0d80b7.

[clang] ae80024 - [clang] Honor __attribute__((no_builtin("foo"))) on functions

2022-05-20 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-05-20T06:41:47-07:00
New Revision: ae80024fbe51339afabfa2ff43ae532356fa3c93

URL: 
https://github.com/llvm/llvm-project/commit/ae80024fbe51339afabfa2ff43ae532356fa3c93
DIFF: 
https://github.com/llvm/llvm-project/commit/ae80024fbe51339afabfa2ff43ae532356fa3c93.diff

LOG: [clang] Honor __attribute__((no_builtin("foo"))) on functions

Support for `__attribute__((no_builtin("foo")))` was added in 
https://reviews.llvm.org/D68028,
but builtins were still being used even when the attribute was placed on a 
function.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D124701

Added: 
clang/test/CodeGen/no-builtin-2.c

Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/AttrDocs.td
clang/lib/CodeGen/CGExpr.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 69b5dc1dd1fd..5d1b12504f78 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -318,6 +318,10 @@ Attribute Changes in Clang
 - The ``__declspec(naked)`` attribute can no longer be written on a member
   function in Microsoft compatibility mode, matching the behavior of cl.exe.
 
+- Attribute ``no_builtin`` should now affect the generated code. It now 
disables
+  builtins (corresponding to the specific names listed in the attribute) in the
+  body of the function the attribute is on.
+
 Windows Support
 ---
 

diff  --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index d635da6b84b8..aedf11a70753 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -6000,9 +6000,6 @@ attribute `clang_builtin_alias`.
 def NoBuiltinDocs : Documentation {
   let Category = DocCatFunction;
   let Content = [{
-.. Note:: This attribute is not yet fully implemented, it is validated but has
-  no effect on the generated code.
-
 The ``__attribute__((no_builtin))`` is similar to the ``-fno-builtin`` flag
 except it is specific to the body of a function. The attribute may also be
 applied to a virtual function but has no effect on the behavior of overriding

diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index fbe1586001b1..3d7f13aed0ab 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5034,7 +5034,16 @@ static CGCallee EmitDirectCallee(CodeGenFunction &CGF, 
GlobalDecl GD) {
   const FunctionDecl *FD = cast(GD.getDecl());
 
   if (auto builtinID = FD->getBuiltinID()) {
+std::string NoBuiltinFD = ("no-builtin-" + FD->getName()).str();
+std::string NoBuiltins = "no-builtins";
 std::string FDInlineName = (FD->getName() + ".inline").str();
+
+bool IsPredefinedLibFunction =
+CGF.getContext().BuiltinInfo.isPredefinedLibFunction(builtinID);
+bool HasAttributeNoBuiltin =
+CGF.CurFn->getAttributes().hasFnAttr(NoBuiltinFD) ||
+CGF.CurFn->getAttributes().hasFnAttr(NoBuiltins);
+
 // When directing calling an inline builtin, call it through it's mangled
 // name to make it clear it's not the actual builtin.
 if (CGF.CurFn->getName() != FDInlineName &&
@@ -5054,8 +5063,11 @@ static CGCallee EmitDirectCallee(CodeGenFunction &CGF, 
GlobalDecl GD) {
 
 // Replaceable builtins provide their own implementation of a builtin. If 
we
 // are in an inline builtin implementation, avoid trivial infinite
-// recursion.
-else
+// recursion. Honor __attribute__((no_builtin("foo"))) or
+// __attribute__((no_builtin)) on the current function unless foo is
+// not a predefined library function which means we must generate the
+// builtin no matter what.
+else if (!IsPredefinedLibFunction || !HasAttributeNoBuiltin)
   return CGCallee::forBuiltin(builtinID, FD);
   }
 

diff  --git a/clang/test/CodeGen/no-builtin-2.c 
b/clang/test/CodeGen/no-builtin-2.c
new file mode 100644
index ..f236f29779e0
--- /dev/null
+++ b/clang/test/CodeGen/no-builtin-2.c
@@ -0,0 +1,63 @@
+// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
+
+typedef typeof(sizeof(0)) size_t;
+
+void bar(char *s);
+void *memset(void *s, int c, size_t n);
+void *memcpy(void *d, const void *s, size_t n);
+void *memmove(void *d, const void *s, size_t n);
+
+// CHECK: define{{.*}} void @foo1({{.*}}) #[[NO_NOBUILTIN:[0-9]+]]
+// CHECK:   call void @bar
+// CHECK:   call void @llvm.memset
+// CHECK:   call void @llvm.memcpy
+// CHECK:   call void @llvm.memmove
+void foo1(char *s, char *d, size_t n) {
+  bar(s);
+  memset(s, 0, n);
+  memcpy(d, s, n);
+  memmove(d, s, n);
+}
+
+// CHECK: define{{.*}} void @foo2({{.*}}) #[[NOBUILTIN_MEMSET:[0-9]+]]
+// CHECK:   call void @bar
+// CHECK:   {{.*}}call {{.*}} @memset
+// CHECK:   call void @llvm.memcpy
+// CHECK:   call void @llvm.memmove
+void foo2(char *s, char *d, size_t n) __attribute__((no_builtin("memset"))) {
+  bar(

[clang] 3e0be56 - [MSVC, ARM64] Add __writex18 intrinsics

2022-05-23 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-05-23T07:01:11-07:00
New Revision: 3e0be5610ff0e9d5bb15f2872bac8cd17bfcc34a

URL: 
https://github.com/llvm/llvm-project/commit/3e0be5610ff0e9d5bb15f2872bac8cd17bfcc34a
DIFF: 
https://github.com/llvm/llvm-project/commit/3e0be5610ff0e9d5bb15f2872bac8cd17bfcc34a.diff

LOG: [MSVC, ARM64] Add __writex18 intrinsics

https://docs.microsoft.com/en-us/cpp/intrinsics/arm64-intrinsics?view=msvc-170

  void __writex18byte(unsigned long, unsigned char)
  void __writex18word(unsigned long, unsigned short)
  void __writex18dword(unsigned long, unsigned long)
  void __writex18qword(unsigned long, unsigned __int64)

Given the lack of documentation of the intrinsics, we chose to align the offset 
with just
`CharUnits::One()` when calling `IRBuilderBase::CreateAlignedStore()`.

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D126023

Added: 


Modified: 
clang/include/clang/Basic/BuiltinsAArch64.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Headers/intrin.h
clang/test/CodeGen/arm64-microsoft-intrinsics.c

Removed: 




diff  --git a/clang/include/clang/Basic/BuiltinsAArch64.def 
b/clang/include/clang/Basic/BuiltinsAArch64.def
index cebd1c07fbcc..a04b48dd128e 100644
--- a/clang/include/clang/Basic/BuiltinsAArch64.def
+++ b/clang/include/clang/Basic/BuiltinsAArch64.def
@@ -251,6 +251,11 @@ TARGET_HEADER_BUILTIN(__umulh, "ULLiULLiULLi", "nh", 
"intrin.h", ALL_MS_LANGUAGE
 
 TARGET_HEADER_BUILTIN(__break, "vi", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
 
+TARGET_HEADER_BUILTIN(__writex18byte,  "vULiUc", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+TARGET_HEADER_BUILTIN(__writex18word,  "vULiUs", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+TARGET_HEADER_BUILTIN(__writex18dword, "vULiULi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+TARGET_HEADER_BUILTIN(__writex18qword, "vULiULLi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+
 #undef BUILTIN
 #undef LANGBUILTIN
 #undef TARGET_HEADER_BUILTIN

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2ee734550dc1..6168ba938db4 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -9952,6 +9952,31 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned 
BuiltinID,
 return HigherBits;
   }
 
+  if (BuiltinID == AArch64::BI__writex18byte ||
+  BuiltinID == AArch64::BI__writex18word ||
+  BuiltinID == AArch64::BI__writex18dword ||
+  BuiltinID == AArch64::BI__writex18qword) {
+llvm::Type *IntTy = ConvertType(E->getArg(1)->getType());
+
+// Read x18 as i8*
+LLVMContext &Context = CGM.getLLVMContext();
+llvm::Metadata *Ops[] = {llvm::MDString::get(Context, "x18")};
+llvm::MDNode *RegName = llvm::MDNode::get(Context, Ops);
+llvm::Value *Metadata = llvm::MetadataAsValue::get(Context, RegName);
+llvm::Function *F =
+CGM.getIntrinsic(llvm::Intrinsic::read_register, {Int64Ty});
+llvm::Value *X18 = Builder.CreateCall(F, Metadata);
+X18 = Builder.CreateIntToPtr(X18, llvm::PointerType::get(Int8Ty, 0));
+
+// Store val at x18 + offset
+Value *Offset = Builder.CreateZExt(EmitScalarExpr(E->getArg(0)), Int64Ty);
+Value *Ptr = Builder.CreateGEP(Int8Ty, X18, Offset);
+Ptr = Builder.CreatePointerCast(Ptr, llvm::PointerType::get(IntTy, 0));
+Value *Val = EmitScalarExpr(E->getArg(1));
+StoreInst *Store = Builder.CreateAlignedStore(Val, Ptr, CharUnits::One());
+return Store;
+  }
+
   // Handle MSVC intrinsics before argument evaluation to prevent double
   // evaluation.
   if (Optional MsvcIntId = translateAarch64ToMsvcIntrin(BuiltinID))

diff  --git a/clang/lib/Headers/intrin.h b/clang/lib/Headers/intrin.h
index 07fcae36020d..dbc5159853dd 100644
--- a/clang/lib/Headers/intrin.h
+++ b/clang/lib/Headers/intrin.h
@@ -562,6 +562,11 @@ __int64 __mulh(__int64 __a, __int64 __b);
 unsigned __int64 __umulh(unsigned __int64 __a, unsigned __int64 __b);
 
 void __break(int);
+
+void __writex18byte(unsigned long offset, unsigned char data);
+void __writex18word(unsigned long offset, unsigned short data);
+void __writex18dword(unsigned long offset, unsigned long data);
+void __writex18qword(unsigned long offset, unsigned __int64 data);
 #endif
 
 
/**\

diff  --git a/clang/test/CodeGen/arm64-microsoft-intrinsics.c 
b/clang/test/CodeGen/arm64-microsoft-intrinsics.c
index ecf271bae580..a9b1d444553f 100644
--- a/clang/test/CodeGen/arm64-microsoft-intrinsics.c
+++ b/clang/test/CodeGen/arm64-microsoft-intrinsics.c
@@ -119,5 +119,73 @@ unsigned __int64 check__getReg(void) {
 
 // CHECK-MSVC: call i64 @llvm.read_register.i64(metadata ![[MD2:.*]])
 // CHECK-MSVC: call i64 @llvm.read_register.i64(metadata ![[MD3:.*]])
+
+void check__writex18byte(unsigned long offset, unsigned char data) {
+  __writex18byte(offset, data);
+}
+
+// CHECK-MSVC: %[[DATA_ADDR:.

[clang] 4f1e64b - [MSVC, ARM64] Add __readx18 intrinsics

2022-05-23 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-05-23T10:59:12-07:00
New Revision: 4f1e64b54f59dd4c303d04b62926f35bde5a2c79

URL: 
https://github.com/llvm/llvm-project/commit/4f1e64b54f59dd4c303d04b62926f35bde5a2c79
DIFF: 
https://github.com/llvm/llvm-project/commit/4f1e64b54f59dd4c303d04b62926f35bde5a2c79.diff

LOG: [MSVC, ARM64] Add __readx18 intrinsics

https://docs.microsoft.com/en-us/cpp/intrinsics/arm64-intrinsics?view=msvc-170

  unsigned char __readx18byte(unsigned long)
  unsigned short __readx18word(unsigned long)
  unsigned long __readx18dword(unsigned long)
  unsigned __int64 __readx18qword(unsigned long)

Given the lack of documentation of the intrinsics, we chose to align the offset 
with just
`CharUnits::One()` when calling `IRBuilderBase::CreateAlignedLoad()`

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D126024

Added: 


Modified: 
clang/include/clang/Basic/BuiltinsAArch64.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Headers/intrin.h
clang/test/CodeGen/arm64-microsoft-intrinsics.c

Removed: 




diff  --git a/clang/include/clang/Basic/BuiltinsAArch64.def 
b/clang/include/clang/Basic/BuiltinsAArch64.def
index a04b48dd128e3..65ab4fcced9ae 100644
--- a/clang/include/clang/Basic/BuiltinsAArch64.def
+++ b/clang/include/clang/Basic/BuiltinsAArch64.def
@@ -256,6 +256,11 @@ TARGET_HEADER_BUILTIN(__writex18word,  "vULiUs", "nh", 
"intrin.h", ALL_MS_LANGUA
 TARGET_HEADER_BUILTIN(__writex18dword, "vULiULi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
 TARGET_HEADER_BUILTIN(__writex18qword, "vULiULLi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
 
+TARGET_HEADER_BUILTIN(__readx18byte,  "UcULi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+TARGET_HEADER_BUILTIN(__readx18word,  "UsULi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+TARGET_HEADER_BUILTIN(__readx18dword, "ULiULi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+TARGET_HEADER_BUILTIN(__readx18qword, "ULLiULi", "nh", "intrin.h", 
ALL_MS_LANGUAGES, "")
+
 #undef BUILTIN
 #undef LANGBUILTIN
 #undef TARGET_HEADER_BUILTIN

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 6168ba938db4f..bdc638299c4bd 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -9977,6 +9977,30 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned 
BuiltinID,
 return Store;
   }
 
+  if (BuiltinID == AArch64::BI__readx18byte ||
+  BuiltinID == AArch64::BI__readx18word ||
+  BuiltinID == AArch64::BI__readx18dword ||
+  BuiltinID == AArch64::BI__readx18qword) {
+llvm::Type *IntTy = ConvertType(E->getType());
+
+// Read x18 as i8*
+LLVMContext &Context = CGM.getLLVMContext();
+llvm::Metadata *Ops[] = {llvm::MDString::get(Context, "x18")};
+llvm::MDNode *RegName = llvm::MDNode::get(Context, Ops);
+llvm::Value *Metadata = llvm::MetadataAsValue::get(Context, RegName);
+llvm::Function *F =
+CGM.getIntrinsic(llvm::Intrinsic::read_register, {Int64Ty});
+llvm::Value *X18 = Builder.CreateCall(F, Metadata);
+X18 = Builder.CreateIntToPtr(X18, llvm::PointerType::get(Int8Ty, 0));
+
+// Load x18 + offset
+Value *Offset = Builder.CreateZExt(EmitScalarExpr(E->getArg(0)), Int64Ty);
+Value *Ptr = Builder.CreateGEP(Int8Ty, X18, Offset);
+Ptr = Builder.CreatePointerCast(Ptr, llvm::PointerType::get(IntTy, 0));
+LoadInst *Load = Builder.CreateAlignedLoad(IntTy, Ptr, CharUnits::One());
+return Load;
+  }
+
   // Handle MSVC intrinsics before argument evaluation to prevent double
   // evaluation.
   if (Optional MsvcIntId = translateAarch64ToMsvcIntrin(BuiltinID))

diff  --git a/clang/lib/Headers/intrin.h b/clang/lib/Headers/intrin.h
index dbc5159853ddf..de68b07491c6c 100644
--- a/clang/lib/Headers/intrin.h
+++ b/clang/lib/Headers/intrin.h
@@ -567,6 +567,11 @@ void __writex18byte(unsigned long offset, unsigned char 
data);
 void __writex18word(unsigned long offset, unsigned short data);
 void __writex18dword(unsigned long offset, unsigned long data);
 void __writex18qword(unsigned long offset, unsigned __int64 data);
+
+unsigned char __readx18byte(unsigned long offset);
+unsigned short __readx18word(unsigned long offset);
+unsigned long __readx18dword(unsigned long offset);
+unsigned __int64 __readx18qword(unsigned long offset);
 #endif
 
 
/**\

diff  --git a/clang/test/CodeGen/arm64-microsoft-intrinsics.c 
b/clang/test/CodeGen/arm64-microsoft-intrinsics.c
index a9b1d444553f5..1ad5233bb4c81 100644
--- a/clang/test/CodeGen/arm64-microsoft-intrinsics.c
+++ b/clang/test/CodeGen/arm64-microsoft-intrinsics.c
@@ -187,5 +187,64 @@ void check__writex18qword(unsigned long offset, unsigned 
__int64 data) {
 // CHECK-MSVC: %[[DATA:.*]] = load i64, i64* %[[DATA_ADDR]], align 8
 // CHECK-MSVC: store i64 %[[DATA]], i64* %[[BITCAST_PTR]], align 1
 
+unsigned char check__readx18byte(

[clang] ee6ad7a - [clang-cl][MSVC] Map /external:Wn n=1-4 to -Wsystem-headers

2022-06-13 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-06-13T11:26:07-07:00
New Revision: ee6ad7af45a0be36191e0d55f005d9a0dd8005be

URL: 
https://github.com/llvm/llvm-project/commit/ee6ad7af45a0be36191e0d55f005d9a0dd8005be
DIFF: 
https://github.com/llvm/llvm-project/commit/ee6ad7af45a0be36191e0d55f005d9a0dd8005be.diff

LOG: [clang-cl][MSVC] Map /external:Wn n=1-4 to -Wsystem-headers

https://docs.microsoft.com/en-us/cpp/build/reference/external-external-headers-diagnostics?view=msvc-170

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D127452

Added: 


Modified: 
clang/include/clang/Driver/Options.td
clang/test/Driver/cl-options.c

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index a5a46bfb3cf0..563ad4611c17 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -775,6 +775,8 @@ def Wa_COMMA : CommaJoined<["-"], "Wa,">,
   MetaVarName<"">;
 def Wall : Flag<["-"], "Wall">, Group, Flags<[CC1Option, HelpHidden]>;
 def WCL4 : Flag<["-"], "WCL4">, Group, Flags<[CC1Option, HelpHidden]>;
+def Wsystem_headers : Flag<["-"], "Wsystem-headers">, Group, 
Flags<[CC1Option, HelpHidden]>;
+def Wno_system_headers : Flag<["-"], "Wno-system-headers">, Group, 
Flags<[CC1Option, HelpHidden]>;
 def Wdeprecated : Flag<["-"], "Wdeprecated">, Group, 
Flags<[CC1Option]>,
   HelpText<"Enable warnings for deprecated constructs and define 
__DEPRECATED">;
 def Wno_deprecated : Flag<["-"], "Wno-deprecated">, Group, 
Flags<[CC1Option]>;
@@ -6475,6 +6477,11 @@ def _SLASH_validate_charset : CLFlag<"validate-charset">,
   Alias, AliasArgs<["invalid-source-encoding"]>;
 def _SLASH_validate_charset_ : CLFlag<"validate-charset-">,
   Alias, AliasArgs<["no-invalid-source-encoding"]>;
+def _SLASH_external_W0 : CLFlag<"external:W0">, HelpText<"Ignore warnings from 
system headers (default)">, Alias;
+def _SLASH_external_W1 : CLFlag<"external:W1">, HelpText<"Enable 
-Wsystem-headers">, Alias;
+def _SLASH_external_W2 : CLFlag<"external:W2">, HelpText<"Enable 
-Wsystem-headers">, Alias;
+def _SLASH_external_W3 : CLFlag<"external:W3">, HelpText<"Enable 
-Wsystem-headers">, Alias;
+def _SLASH_external_W4 : CLFlag<"external:W4">, HelpText<"Enable 
-Wsystem-headers">, Alias;
 def _SLASH_W0 : CLFlag<"W0">, HelpText<"Disable all warnings">, Alias;
 def _SLASH_W1 : CLFlag<"W1">, HelpText<"Enable -Wall">, Alias;
 def _SLASH_W2 : CLFlag<"W2">, HelpText<"Enable -Wall">, Alias;

diff  --git a/clang/test/Driver/cl-options.c b/clang/test/Driver/cl-options.c
index 1e518e0035ae..89a5a90dfa57 100644
--- a/clang/test/Driver/cl-options.c
+++ b/clang/test/Driver/cl-options.c
@@ -752,4 +752,12 @@
 // RUN: %clang_cl /JMC /Z7 /c -### -- %s 2>&1 | FileCheck %s --check-prefix JMC
 // JMC: -fjmc
 
+// RUN: %clang_cl /external:W0 /c -### -- %s 2>&1 | FileCheck 
-check-prefix=EXTERNAL_W0 %s
+// RUN: %clang_cl /external:W1 /c -### -- %s 2>&1 | FileCheck 
-check-prefix=EXTERNAL_Wn %s
+// RUN: %clang_cl /external:W2 /c -### -- %s 2>&1 | FileCheck 
-check-prefix=EXTERNAL_Wn %s
+// RUN: %clang_cl /external:W3 /c -### -- %s 2>&1 | FileCheck 
-check-prefix=EXTERNAL_Wn %s
+// RUN: %clang_cl /external:W4 /c -### -- %s 2>&1 | FileCheck 
-check-prefix=EXTERNAL_Wn %s
+// EXTERNAL_W0: "-Wno-system-headers"
+// EXTERNAL_Wn: "-Wsystem-headers"
+
 void f(void) { }



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] d4245ed - [clang-cl] Accept /FA[c][s][u], but ignore the arguments

2022-06-13 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-06-13T12:01:54-07:00
New Revision: d4245ed67ce44188714e6b29a0b45e860619bcf2

URL: 
https://github.com/llvm/llvm-project/commit/d4245ed67ce44188714e6b29a0b45e860619bcf2
DIFF: 
https://github.com/llvm/llvm-project/commit/d4245ed67ce44188714e6b29a0b45e860619bcf2.diff

LOG: [clang-cl] Accept /FA[c][s][u], but ignore the arguments

Previously, /FAsc would emit a warning. Now, it will just do what /FA does.

https://docs.microsoft.com/en-us/cpp/build/reference/fa-fa-listing-file?view=msvc-170

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D127519

Added: 


Modified: 
clang/include/clang/Driver/Options.td
clang/test/Driver/cl-options.c
clang/test/Driver/cl-outputs.c

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 563ad4611c17..b614ae969c62 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6582,7 +6582,7 @@ def _SLASH_EP : CLFlag<"EP">,
 def _SLASH_external_env : CLJoined<"external:env:">,
   HelpText<"Add dirs in env var  to include search path with warnings 
suppressed">,
   MetaVarName<"">;
-def _SLASH_FA : CLFlag<"FA">,
+def _SLASH_FA : CLJoined<"FA">,
   HelpText<"Output assembly code file during compilation">;
 def _SLASH_Fa : CLJoined<"Fa">,
   HelpText<"Set assembly output file name (with /FA)">,
@@ -6750,7 +6750,6 @@ def _SLASH_doc : CLJoined<"doc">;
 def _SLASH_experimental : CLJoined<"experimental:">;
 def _SLASH_exportHeader : CLFlag<"exportHeader">;
 def _SLASH_external : CLJoined<"external:">;
-def _SLASH_FA_joined : CLJoined<"FA">;
 def _SLASH_favor : CLJoined<"favor">;
 def _SLASH_fsanitize_address_use_after_return : 
CLJoined<"fsanitize-address-use-after-return">;
 def _SLASH_fno_sanitize_address_vcasan_lib : 
CLJoined<"fno-sanitize-address-vcasan-lib">;

diff  --git a/clang/test/Driver/cl-options.c b/clang/test/Driver/cl-options.c
index 89a5a90dfa57..a2e350a0a835 100644
--- a/clang/test/Driver/cl-options.c
+++ b/clang/test/Driver/cl-options.c
@@ -65,20 +65,29 @@
 
 // RUN: %clang_cl -### /FA -fprofile-instr-generate -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-INSTR-GENERATE %s
 // RUN: %clang_cl -### /FA -fprofile-instr-generate=/tmp/somefile.profraw -- 
%s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-INSTR-GENERATE-FILE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-instr-generate -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-INSTR-GENERATE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-instr-generate=/tmp/somefile.profraw 
-- %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-INSTR-GENERATE-FILE %s
 // CHECK-PROFILE-INSTR-GENERATE: "-fprofile-instrument=clang" 
"--dependent-lib=clang_rt.profile{{[^"]*}}.lib"
 // CHECK-PROFILE-INSTR-GENERATE-FILE: 
"-fprofile-instrument-path=/tmp/somefile.profraw"
 
 // RUN: %clang_cl -### /FA -fprofile-generate -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-GENERATE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-generate -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-GENERATE %s
 // CHECK-PROFILE-GENERATE: "-fprofile-instrument=llvm" 
"--dependent-lib=clang_rt.profile{{[^"]*}}.lib"
 
 // RUN: %clang_cl -### /FA -fprofile-instr-generate -fprofile-instr-use -- %s 
2>&1 | FileCheck -check-prefix=CHECK-NO-MIX-GEN-USE %s
 // RUN: %clang_cl -### /FA -fprofile-instr-generate -fprofile-instr-use=file 
-- %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MIX-GEN-USE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-instr-generate -fprofile-instr-use -- 
%s 2>&1 | FileCheck -check-prefix=CHECK-NO-MIX-GEN-USE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-instr-generate 
-fprofile-instr-use=file -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-NO-MIX-GEN-USE %s
 // CHECK-NO-MIX-GEN-USE: '{{[a-z=-]*}}' not allowed with '{{[a-z=-]*}}'
 
 // RUN: %clang_cl -### /FA -fprofile-instr-use -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-USE %s
 // RUN: %clang_cl -### /FA -fprofile-use -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-USE %s
 // RUN: %clang_cl -### /FA -fprofile-instr-use=/tmp/somefile.prof -- %s 2>&1 | 
FileCheck -check-prefix=CHECK-PROFILE-USE-FILE %s
 // RUN: %clang_cl -### /FA -fprofile-use=/tmp/somefile.prof -- %s 2>&1 | 
FileCheck -check-prefix=CHECK-PROFILE-USE-FILE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-instr-use -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-USE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-use -- %s 2>&1 | FileCheck 
-check-prefix=CHECK-PROFILE-USE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-instr-use=/tmp/somefile.prof -- %s 
2>&1 | FileCheck -check-prefix=CHECK-PROFILE-USE-FILE %s
+// RUN: %clang_cl -### /FAcsu -fprofile-use=/tmp/somefile.prof -- %s 2>&1 | 
FileCheck -check-prefix=CHECK-PROFILE-USE-FILE %s
 // CHECK-PROFILE-USE: "-fprofile-instrument-use-path=default.profdata"
 // CHECK-PROFILE-USE-FILE: "-fprofile-instrument-use-path=/tmp/somefile.prof"
 

diff  --

[clang] 186bea3 - [MSVC] Add initial support for MSVC pragma optimize

2022-06-24 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-06-24T08:03:42-07:00
New Revision: 186bea3750d6b349de0e71044d95e2a42e087b4c

URL: 
https://github.com/llvm/llvm-project/commit/186bea3750d6b349de0e71044d95e2a42e087b4c
DIFF: 
https://github.com/llvm/llvm-project/commit/186bea3750d6b349de0e71044d95e2a42e087b4c.diff

LOG: [MSVC] Add initial support for MSVC pragma optimize

MSVC's pragma optimize turns optimizations on or off based on the list
passed. At the moment, we only support an empty optimization list.

i.e. `#pragma optimize("", on | off)`

>From MSVC's docs:

| Parameter | Type of optimization |
|---|--|
| g | Enable global optimizations. Deprecated  |
| s or t| Specify short or fast sequences of machine code  |
| y | Generate frame pointers on the program stack |

https://docs.microsoft.com/en-us/cpp/preprocessor/optimize?view=msvc-170

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D125723

Added: 
clang/test/CodeGen/pragma-msvc-optimize.c

Modified: 
clang/docs/LanguageExtensions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticParseKinds.td
clang/include/clang/Parse/Parser.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParsePragma.cpp
clang/lib/Sema/SemaAttr.cpp
clang/lib/Sema/SemaDecl.cpp
clang/test/Preprocessor/pragma_microsoft.c

Removed: 




diff  --git a/clang/docs/LanguageExtensions.rst 
b/clang/docs/LanguageExtensions.rst
index a69b798f31ee4..59c9889df9622 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -3813,6 +3813,43 @@ it causes the instantiation of ``twice`` and ``thrice`` 
with an ``int`` type; of
 these two instantiations, ``twice`` will be optimized (because its definition
 was outside the region) and ``thrice`` will not be optimized.
 
+Clang also implements MSVC's range-based pragma,
+``#pragma optimize("[optimization-list]", on | off)``. At the moment, Clang 
only
+supports an empty optimization list, whereas MSVC supports the arguments, 
``s``,
+``g``, ``t``, and ``y``. Currently, the implementation of ``pragma optimize`` 
behaves
+the same as ``#pragma clang optimize``. All functions
+between ``off`` and ``on`` will be decorated with the ``optnone`` attribute.
+
+.. code-block:: c++
+
+  #pragma optimize("", off)
+  // This function will be decorated with optnone.
+  void f1() {}
+
+  #pragma optimize("", on)
+  // This function will be optimized with whatever was specified on
+  // the commandline.
+  void f2() {}
+
+  // This will warn with Clang's current implementation.
+  #pragma optimize("g", on)
+  void f3() {}
+
+For MSVC, an empty optimization list and ``off`` parameter will turn off
+all optimizations, ``s``, ``g``, ``t``, and ``y``. An empty optimization and
+``on`` parameter will reset the optimizations to the ones specified on the
+commandline.
+
+.. list-table:: Parameters (unsupported by Clang)
+   * - Parameter
+ - Type of optimization
+   * - g
+ - Deprecated
+   * - s or t
+ - Short or fast sequences of machine code
+   * - y
+ - Enable frame pointers
+
 Extensions for loop hint optimizations
 ==
 

diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1b0bd5e52b8fb..c884f745b8fc9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -336,6 +336,10 @@ New Pragmas in Clang
 - Added support for MSVC's ``#pragma alloc_text``. The pragma names the code
   section functions are placed in. The pragma only applies to functions with
   C linkage.
+- Added support for an empty optimization list for MSVC's ``#pragma optimize``.
+  The pragma takes a list of optimizations to turn on or off which applies to
+  all functions following the pragma. At the moment, only an empty list is
+  supported.
 
 - ...
 

diff  --git a/clang/include/clang/Basic/DiagnosticParseKinds.td 
b/clang/include/clang/Basic/DiagnosticParseKinds.td
index b0abe2aa517e7..352a050ba5cf1 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1169,10 +1169,6 @@ def warn_pragma_pack_malformed : Warning<
 def warn_pragma_intrinsic_builtin : Warning<
   "%0 is not a recognized builtin%select{|; consider including  to 
access non-builtin intrinsics}1">,
   InGroup;
-// - #pragma optimize
-def warn_pragma_optimize : Warning<
-  "'#pragma optimize' is not supported">,
-  InGroup;
 // - #pragma unused
 def warn_pragma_unused_expected_var : Warning<
   "expected '#pragma unused' argument to be a variable name">,

diff  --git a/clang/include/clang/Parse/Parser.h 
b/clang/include/clang/Parse/Parser.h
index 0eb6f7104a55b..76e1c9db5284e 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -728,6 +728,

[clang] a5b056f - [MSVC] Fix pragma alloc_text failing for C files

2022-06-01 Thread Stephen Long via cfe-commits

Author: Stephen Long
Date: 2022-06-01T09:39:46-07:00
New Revision: a5b056fe49a991c65e665468f1a681965f41b137

URL: 
https://github.com/llvm/llvm-project/commit/a5b056fe49a991c65e665468f1a681965f41b137
DIFF: 
https://github.com/llvm/llvm-project/commit/a5b056fe49a991c65e665468f1a681965f41b137.diff

LOG: [MSVC] Fix pragma alloc_text failing for C files

`isExternCContext()` is returning false for functions in C files

Reviewed By: rnk, aaron.ballman

Differential Revision: https://reviews.llvm.org/D126559

Added: 
clang/test/Sema/pragma-ms-alloc-text.c

Modified: 
clang/lib/Sema/SemaAttr.cpp
clang/test/Sema/pragma-ms-alloc-text.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 9681117abe424..4560bb060a4aa 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -763,7 +763,7 @@ void Sema::ActOnPragmaMSAllocText(
 }
 
 DeclContext *DC = ND->getDeclContext();
-if (!DC->isExternCContext()) {
+if (getLangOpts().CPlusPlus && !DC->isExternCContext()) {
   Diag(Loc, diag::err_pragma_alloc_text_c_linkage);
   return;
 }

diff  --git a/clang/test/Sema/pragma-ms-alloc-text.c 
b/clang/test/Sema/pragma-ms-alloc-text.c
new file mode 100644
index 0..a5f2e9f11dce2
--- /dev/null
+++ b/clang/test/Sema/pragma-ms-alloc-text.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify %s
+
+void foo();
+#pragma alloc_text("hello", foo) // expected-no-diagnostics
+void foo() {}
+
+static void foo1();
+#pragma alloc_text("hello", foo1) // expected-no-diagnostics
+void foo1() {}

diff  --git a/clang/test/Sema/pragma-ms-alloc-text.cpp 
b/clang/test/Sema/pragma-ms-alloc-text.cpp
index e1fb0ab4b2538..931b152fe78cc 100644
--- a/clang/test/Sema/pragma-ms-alloc-text.cpp
+++ b/clang/test/Sema/pragma-ms-alloc-text.cpp
@@ -34,3 +34,9 @@ void foo4() {}
 void foo5();// expected-note {{previous declaration is here}}
 #pragma alloc_text(c, foo5) // expected-error {{'#pragma alloc_text' is 
applicable only to functions with C linkage}}
 extern "C" void foo5() {}   // expected-error {{declaration of 'foo5' has a 
diff erent language linkage}}
+
+extern "C" {
+static void foo6();
+#pragma alloc_text(c, foo6) // no-warning
+void foo6() {}
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] e3a6784 - [clang-cl] Add support for /kernel

2022-06-07 Thread Stephen Long via cfe-commits

Author: Pengxuan Zheng
Date: 2022-06-07T06:42:35-07:00
New Revision: e3a6784ac9672506ba69c1754a5e6b7712e1fba7

URL: 
https://github.com/llvm/llvm-project/commit/e3a6784ac9672506ba69c1754a5e6b7712e1fba7
DIFF: 
https://github.com/llvm/llvm-project/commit/e3a6784ac9672506ba69c1754a5e6b7712e1fba7.diff

LOG: [clang-cl] Add support for /kernel

MSVC defines _KERNEL_MODE when /kernel is passed.
Also, /kernel disables RTTI and C++ exception handling.

https://docs.microsoft.com/en-us/cpp/build/reference/kernel-create-kernel-mode-binary?view=msvc-170

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D126719

Added: 


Modified: 
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Driver/Options.td
clang/lib/Basic/Targets/OSTargets.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/cl-zc.cpp

Removed: 




diff  --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6e1a01c6c9767..9c4d639178145 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -88,6 +88,7 @@ LANGOPT(C11   , 1, 0, "C11")
 LANGOPT(C17   , 1, 0, "C17")
 LANGOPT(C2x   , 1, 0, "C2x")
 LANGOPT(MSVCCompat, 1, 0, "Microsoft Visual C++ full compatibility 
mode")
+LANGOPT(Kernel, 1, 0, "Kernel mode")
 LANGOPT(MicrosoftExt  , 1, 0, "Microsoft C++ extensions")
 LANGOPT(AsmBlocks , 1, 0, "Microsoft inline asm blocks")
 LANGOPT(Borland   , 1, 0, "Borland extensions")

diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 1cef95a0d4efe..b2feed0e36329 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2178,6 +2178,8 @@ def fms_memptr_rep_EQ : Joined<["-"], "fms-memptr-rep=">, 
Group, Flags<
   NormalizedValues<["PPTMK_FullGeneralitySingleInheritance", 
"PPTMK_FullGeneralityMultipleInheritance",
 "PPTMK_FullGeneralityVirtualInheritance"]>,
   MarshallingInfoEnum, 
"PPTMK_BestCase">;
+def fms_kernel : Flag<["-"], "fms-kernel">, Group, Flags<[CC1Option, 
NoDriverOption]>,
+  MarshallingInfoFlag>;
 // __declspec is enabled by default for the PS4 by the driver, and also
 // enabled for Microsoft Extensions or Borland Extensions, here.
 //

diff  --git a/clang/lib/Basic/Targets/OSTargets.cpp 
b/clang/lib/Basic/Targets/OSTargets.cpp
index 6cf78fd4f2441..f79e20779a3d1 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -215,6 +215,9 @@ static void addVisualCDefines(const LangOptions &Opts, 
MacroBuilder &Builder) {
 }
   }
 
+  if (Opts.Kernel)
+Builder.defineMacro("_KERNEL_MODE");
+
   Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
   Builder.defineMacro("__STDC_NO_THREADS__");
 

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 2328e3db1973f..fe79921524727 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -38,6 +38,7 @@
 #include "clang/Driver/SanitizerArgs.h"
 #include "clang/Driver/Types.h"
 #include "clang/Driver/XRayArgs.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Option/ArgList.h"
@@ -7455,6 +7456,12 @@ static EHFlags parseClangCLEHFlags(const Driver &D, 
const ArgList &Args) {
 EH.NoUnwindC = true;
   }
 
+  if (Args.hasArg(options::OPT__SLASH_kernel)) {
+EH.Synch = false;
+EH.NoUnwindC = false;
+EH.Asynch = false;
+  }
+
   return EH;
 }
 
@@ -7599,6 +7606,27 @@ void Clang::AddClangCLArgs(const ArgList &Args, 
types::ID InputType,
CmdArgs.push_back("-fno-wchar");
  }
 
+ if (Args.hasArg(options::OPT__SLASH_kernel)) {
+   llvm::Triple::ArchType Arch = getToolChain().getArch();
+   std::vector Values =
+   Args.getAllArgValues(options::OPT__SLASH_arch);
+   if (!Values.empty()) {
+ llvm::SmallSet SupportedArches;
+ if (Arch == llvm::Triple::x86)
+   SupportedArches.insert("IA32");
+
+ for (auto &V : Values)
+   if (!SupportedArches.contains(V))
+ D.Diag(diag::err_drv_argument_not_allowed_with)
+ << std::string("/arch:").append(V) << "/kernel";
+   }
+
+   CmdArgs.push_back("-fno-rtti");
+   if (Args.hasFlag(options::OPT__SLASH_GR, options::OPT__SLASH_GR_, false))
+ D.Diag(diag::err_drv_argument_not_allowed_with) << "/GR"
+ << "/kernel";
+ }
+
   Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg);
   Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb);
   if (MostGeneralArg && BestCaseArg)
@@ -7668,6 +7696,9 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID 
InputType,
 CmdArgs.push_back("msvc");
   }
 
+  if (Args.hasArg(options::OPT__SLASH_kernel))
+