[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-06-04 Thread Serge Pavlov via cfe-commits

spavloff wrote:

The discussion for the proposed mechanism is open here: 
https://discourse.llvm.org/t/rfc-calling-functions-if-pragma-fenv-round-is-present/79372

> > This change introduces macro ROUNDING_MODE, which is a string dependent on 
> > the constant rounding mode
>
> It expands to an identify, not a string literal, right?

Exactly. 


https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-06-04 Thread Serge Pavlov via cfe-commits

https://github.com/spavloff updated 
https://github.com/llvm/llvm-project/pull/92699

>From f8cd2539fb7f0388d7f3955f58b61b09da03bf0c Mon Sep 17 00:00:00 2001
From: Serge Pavlov 
Date: Sun, 19 May 2024 18:43:08 +0700
Subject: [PATCH 1/4] [clang] Macro for constant rounding mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The forthcoming C standard defines pragma FENV_ROUND to support constant
rounding mode. It also requires some functions to be evaluated with such
mode, N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be
implemented using preprocessor facility. Such implementation would
require a builtin macro that is set in the region where pragma
FENV_ROUND is in effect and reflects constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string
dependent on the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers.
Default value, when no pragma FENV_ROUND is specified, is empty string.
Concatenation of a function name with the builtin macro can be used to
obtain name of the function variant for particular rounding mode, like
"sin_rtz", or "__builtin_cos_rtd". The test "macro_rounding_mode.c"
added in this change provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also
depends on the results of semantic analysis.
---
 clang/include/clang/Lex/Preprocessor.h| 12 
 clang/lib/Lex/PPMacroExpansion.cpp| 25 +
 clang/lib/Sema/Sema.cpp   |  1 +
 clang/lib/Sema/SemaAttr.cpp   |  1 +
 clang/test/Preprocessor/macro_rounding_mode.c | 55 +++
 5 files changed, 94 insertions(+)
 create mode 100644 clang/test/Preprocessor/macro_rounding_mode.c

diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index c0850a8fa9f7f..295633b2e3c73 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -181,6 +181,7 @@ class Preprocessor {
   IdentifierInfo *Ident__is_target_variant_os;
   IdentifierInfo *Ident__is_target_variant_environment;
   IdentifierInfo *Ident__FLT_EVAL_METHOD__;// __FLT_EVAL_METHOD
+  IdentifierInfo *Ident__ROUNDING_MODE__;  // __ROUNDING_MODE__
 
   // Weak, only valid (and set) while InMacroArgs is true.
   Token* ArgMacro;
@@ -201,6 +202,9 @@ class Preprocessor {
   LangOptions::FPEvalMethodKind TUFPEvalMethod =
   LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
 
+  LangOptions::RoundingMode CurrentRoundingMode =
+  LangOptions::RoundingMode::Dynamic;
+
   // Next __COUNTER__ value, starts at 0.
   unsigned CounterValue = 0;
 
@@ -2356,6 +2360,14 @@ class Preprocessor {
 TUFPEvalMethod = Val;
   }
 
+  LangOptions::RoundingMode getCurrentRoundingMode() const {
+return CurrentRoundingMode;
+  }
+
+  void setCurrentRoundingMode(LangOptions::RoundingMode RM) {
+CurrentRoundingMode = RM;
+  }
+
   /// Retrieves the module that we're currently building, if any.
   Module *getCurrentModule();
 
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 8af4a97d00cb8..519fbfd666375 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -344,6 +344,7 @@ void Preprocessor::RegisterBuiltinMacros() {
   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
   Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, 
"__FLT_EVAL_METHOD__");
+  Ident__ROUNDING_MODE__ = RegisterBuiltinMacro(*this, "__ROUNDING_MODE__");
 
   // C++ Standing Document Extensions.
   if (getLangOpts().CPlusPlus)
@@ -1654,6 +1655,30 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
   Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
   Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
 }
+  } else if (II == Ident__ROUNDING_MODE__) {
+switch (getCurrentRoundingMode()) {
+case LangOptions::RoundingMode::TowardZero:
+  OS << "_rtz";
+  break;
+case LangOptions::RoundingMode::NearestTiesToEven:
+ 

[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-27 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

Oh, I somehow thought the macro was part of the spec; reading again, I guess it 
isn't, it's just an attempt to implement the spec.

We probably want some feedback from libc implementers to check if this is what 
they want.  I don't really want to end up in a situation where we end up 
implementing this, but then nobody ends up using it.  Or we end up with two 
almost identical macros for the same thing.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-27 Thread Joshua Cranmer via cfe-commits

jcranmer-intel wrote:

Overall, I'm not opposed to this patch.

This new macro should probably be mentioned somewhere in the clang user 
documentation.

> The way this requirement is formulated indicates that it could be implemented 
> using preprocessor facility. Such implementation would require a builtin 
> macro that is set in the region where pragma FENV_ROUND is in effect and 
> reflects constant rounding mode.

Has there been any discussion with gcc and libc implementations about how they 
plan to implement this requirement? I'm not entirely convinced this is the best 
approach, especially given that "`printf` and `scanf` families" is on the list 
of functions that need to be affected, and that's a decent amount of functions 
to have to wrap with round-mode-aware-variants.

> This change introduces macro ROUNDING_MODE, which is a string dependent on 
> the constant rounding mode

It expands to an identify, not a string literal, right?

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-27 Thread Serge Pavlov via cfe-commits

spavloff wrote:

Thanks! I will commit it in a couple of day, if no additional feedback is 
provided.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-24 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic approved this pull request.

LGTM, but maybe wait a few days to merge in case someone else has comments.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-24 Thread Serge Pavlov via cfe-commits


@@ -965,6 +965,16 @@ void Preprocessor::Lex(Token &Result) {
   LastTokenWasAt = Result.is(tok::at);
   --LexLevel;
 
+  if (Result.is(tok::l_brace)) {
+CurlyBraceLevel++;
+  } else if (Result.is(tok::r_brace)) {
+if (!RoundingPragmas.empty() &&

spavloff wrote:

A good idea, thank you.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-24 Thread Serge Pavlov via cfe-commits

https://github.com/spavloff updated 
https://github.com/llvm/llvm-project/pull/92699

>From f8cd2539fb7f0388d7f3955f58b61b09da03bf0c Mon Sep 17 00:00:00 2001
From: Serge Pavlov 
Date: Sun, 19 May 2024 18:43:08 +0700
Subject: [PATCH 1/3] [clang] Macro for constant rounding mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The forthcoming C standard defines pragma FENV_ROUND to support constant
rounding mode. It also requires some functions to be evaluated with such
mode, N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be
implemented using preprocessor facility. Such implementation would
require a builtin macro that is set in the region where pragma
FENV_ROUND is in effect and reflects constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string
dependent on the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers.
Default value, when no pragma FENV_ROUND is specified, is empty string.
Concatenation of a function name with the builtin macro can be used to
obtain name of the function variant for particular rounding mode, like
"sin_rtz", or "__builtin_cos_rtd". The test "macro_rounding_mode.c"
added in this change provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also
depends on the results of semantic analysis.
---
 clang/include/clang/Lex/Preprocessor.h| 12 
 clang/lib/Lex/PPMacroExpansion.cpp| 25 +
 clang/lib/Sema/Sema.cpp   |  1 +
 clang/lib/Sema/SemaAttr.cpp   |  1 +
 clang/test/Preprocessor/macro_rounding_mode.c | 55 +++
 5 files changed, 94 insertions(+)
 create mode 100644 clang/test/Preprocessor/macro_rounding_mode.c

diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index c0850a8fa9f7f..295633b2e3c73 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -181,6 +181,7 @@ class Preprocessor {
   IdentifierInfo *Ident__is_target_variant_os;
   IdentifierInfo *Ident__is_target_variant_environment;
   IdentifierInfo *Ident__FLT_EVAL_METHOD__;// __FLT_EVAL_METHOD
+  IdentifierInfo *Ident__ROUNDING_MODE__;  // __ROUNDING_MODE__
 
   // Weak, only valid (and set) while InMacroArgs is true.
   Token* ArgMacro;
@@ -201,6 +202,9 @@ class Preprocessor {
   LangOptions::FPEvalMethodKind TUFPEvalMethod =
   LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
 
+  LangOptions::RoundingMode CurrentRoundingMode =
+  LangOptions::RoundingMode::Dynamic;
+
   // Next __COUNTER__ value, starts at 0.
   unsigned CounterValue = 0;
 
@@ -2356,6 +2360,14 @@ class Preprocessor {
 TUFPEvalMethod = Val;
   }
 
+  LangOptions::RoundingMode getCurrentRoundingMode() const {
+return CurrentRoundingMode;
+  }
+
+  void setCurrentRoundingMode(LangOptions::RoundingMode RM) {
+CurrentRoundingMode = RM;
+  }
+
   /// Retrieves the module that we're currently building, if any.
   Module *getCurrentModule();
 
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 8af4a97d00cb8..519fbfd666375 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -344,6 +344,7 @@ void Preprocessor::RegisterBuiltinMacros() {
   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
   Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, 
"__FLT_EVAL_METHOD__");
+  Ident__ROUNDING_MODE__ = RegisterBuiltinMacro(*this, "__ROUNDING_MODE__");
 
   // C++ Standing Document Extensions.
   if (getLangOpts().CPlusPlus)
@@ -1654,6 +1655,30 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
   Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
   Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
 }
+  } else if (II == Ident__ROUNDING_MODE__) {
+switch (getCurrentRoundingMode()) {
+case LangOptions::RoundingMode::TowardZero:
+  OS << "_rtz";
+  break;
+case LangOptions::RoundingMode::NearestTiesToEven:
+ 

[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-24 Thread Eli Friedman via cfe-commits


@@ -965,6 +965,16 @@ void Preprocessor::Lex(Token &Result) {
   LastTokenWasAt = Result.is(tok::at);
   --LexLevel;
 
+  if (Result.is(tok::l_brace)) {
+CurlyBraceLevel++;
+  } else if (Result.is(tok::r_brace)) {
+if (!RoundingPragmas.empty() &&

efriedma-quic wrote:

Maybe we can move the `!RoundingPragmas.empty()` outside the if statement, so 
we can skip a little more of the code if the user isn't using any pragmas?  I 
mean, it probably doesn't make a big difference, but Lex() is 
performance-sensitive.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-24 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic commented:

This approach seems much better.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-24 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic edited 
https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-23 Thread Serge Pavlov via cfe-commits


@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 -emit-llvm -triple i386-linux -Wno-unknown-pragmas %s -o - 
| FileCheck %s

spavloff wrote:

Parsing pragma FENV_ROUND has been moved to the preprocessor.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-23 Thread Serge Pavlov via cfe-commits

https://github.com/spavloff updated 
https://github.com/llvm/llvm-project/pull/92699

>From f8cd2539fb7f0388d7f3955f58b61b09da03bf0c Mon Sep 17 00:00:00 2001
From: Serge Pavlov 
Date: Sun, 19 May 2024 18:43:08 +0700
Subject: [PATCH 1/2] [clang] Macro for constant rounding mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The forthcoming C standard defines pragma FENV_ROUND to support constant
rounding mode. It also requires some functions to be evaluated with such
mode, N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be
implemented using preprocessor facility. Such implementation would
require a builtin macro that is set in the region where pragma
FENV_ROUND is in effect and reflects constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string
dependent on the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers.
Default value, when no pragma FENV_ROUND is specified, is empty string.
Concatenation of a function name with the builtin macro can be used to
obtain name of the function variant for particular rounding mode, like
"sin_rtz", or "__builtin_cos_rtd". The test "macro_rounding_mode.c"
added in this change provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also
depends on the results of semantic analysis.
---
 clang/include/clang/Lex/Preprocessor.h| 12 
 clang/lib/Lex/PPMacroExpansion.cpp| 25 +
 clang/lib/Sema/Sema.cpp   |  1 +
 clang/lib/Sema/SemaAttr.cpp   |  1 +
 clang/test/Preprocessor/macro_rounding_mode.c | 55 +++
 5 files changed, 94 insertions(+)
 create mode 100644 clang/test/Preprocessor/macro_rounding_mode.c

diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index c0850a8fa9f7f..295633b2e3c73 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -181,6 +181,7 @@ class Preprocessor {
   IdentifierInfo *Ident__is_target_variant_os;
   IdentifierInfo *Ident__is_target_variant_environment;
   IdentifierInfo *Ident__FLT_EVAL_METHOD__;// __FLT_EVAL_METHOD
+  IdentifierInfo *Ident__ROUNDING_MODE__;  // __ROUNDING_MODE__
 
   // Weak, only valid (and set) while InMacroArgs is true.
   Token* ArgMacro;
@@ -201,6 +202,9 @@ class Preprocessor {
   LangOptions::FPEvalMethodKind TUFPEvalMethod =
   LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
 
+  LangOptions::RoundingMode CurrentRoundingMode =
+  LangOptions::RoundingMode::Dynamic;
+
   // Next __COUNTER__ value, starts at 0.
   unsigned CounterValue = 0;
 
@@ -2356,6 +2360,14 @@ class Preprocessor {
 TUFPEvalMethod = Val;
   }
 
+  LangOptions::RoundingMode getCurrentRoundingMode() const {
+return CurrentRoundingMode;
+  }
+
+  void setCurrentRoundingMode(LangOptions::RoundingMode RM) {
+CurrentRoundingMode = RM;
+  }
+
   /// Retrieves the module that we're currently building, if any.
   Module *getCurrentModule();
 
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 8af4a97d00cb8..519fbfd666375 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -344,6 +344,7 @@ void Preprocessor::RegisterBuiltinMacros() {
   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
   Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, 
"__FLT_EVAL_METHOD__");
+  Ident__ROUNDING_MODE__ = RegisterBuiltinMacro(*this, "__ROUNDING_MODE__");
 
   // C++ Standing Document Extensions.
   if (getLangOpts().CPlusPlus)
@@ -1654,6 +1655,30 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
   Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
   Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
 }
+  } else if (II == Ident__ROUNDING_MODE__) {
+switch (getCurrentRoundingMode()) {
+case LangOptions::RoundingMode::TowardZero:
+  OS << "_rtz";
+  break;
+case LangOptions::RoundingMode::NearestTiesToEven:
+ 

[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-20 Thread Eli Friedman via cfe-commits


@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 -emit-llvm -triple i386-linux -Wno-unknown-pragmas %s -o - 
| FileCheck %s

efriedma-quic wrote:

Is there some reason the preprocessor can't parse FENV_ROUND?  Breaking code 
with -save-temps etc. seems bad.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-20 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Serge Pavlov (spavloff)


Changes

The forthcoming C standard defines pragma FENV_ROUND to support constant 
rounding mode. It also requires some functions to be evaluated with such mode, 
N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be implemented 
using preprocessor facility. Such implementation would require a builtin macro 
that is set in the region where pragma FENV_ROUND is in effect and reflects 
constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string dependent on 
the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers. Default 
value, when no pragma FENV_ROUND is specified, is empty string. Concatenation 
of a function name with the builtin macro can be used to obtain name of the 
function variant for particular rounding mode, like "sin_rtz", or 
"__builtin_cos_rtd". The test "macro_rounding_mode.c" added in this change 
provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also depends 
on the results of semantic analysis.

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


5 Files Affected:

- (modified) clang/include/clang/Lex/Preprocessor.h (+12) 
- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+25) 
- (modified) clang/lib/Sema/Sema.cpp (+1) 
- (modified) clang/lib/Sema/SemaAttr.cpp (+1) 
- (added) clang/test/Preprocessor/macro_rounding_mode.c (+55) 


``diff
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index c0850a8fa9f7f..295633b2e3c73 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -181,6 +181,7 @@ class Preprocessor {
   IdentifierInfo *Ident__is_target_variant_os;
   IdentifierInfo *Ident__is_target_variant_environment;
   IdentifierInfo *Ident__FLT_EVAL_METHOD__;// __FLT_EVAL_METHOD
+  IdentifierInfo *Ident__ROUNDING_MODE__;  // __ROUNDING_MODE__
 
   // Weak, only valid (and set) while InMacroArgs is true.
   Token* ArgMacro;
@@ -201,6 +202,9 @@ class Preprocessor {
   LangOptions::FPEvalMethodKind TUFPEvalMethod =
   LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
 
+  LangOptions::RoundingMode CurrentRoundingMode =
+  LangOptions::RoundingMode::Dynamic;
+
   // Next __COUNTER__ value, starts at 0.
   unsigned CounterValue = 0;
 
@@ -2356,6 +2360,14 @@ class Preprocessor {
 TUFPEvalMethod = Val;
   }
 
+  LangOptions::RoundingMode getCurrentRoundingMode() const {
+return CurrentRoundingMode;
+  }
+
+  void setCurrentRoundingMode(LangOptions::RoundingMode RM) {
+CurrentRoundingMode = RM;
+  }
+
   /// Retrieves the module that we're currently building, if any.
   Module *getCurrentModule();
 
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 8af4a97d00cb8..519fbfd666375 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -344,6 +344,7 @@ void Preprocessor::RegisterBuiltinMacros() {
   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
   Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, 
"__FLT_EVAL_METHOD__");
+  Ident__ROUNDING_MODE__ = RegisterBuiltinMacro(*this, "__ROUNDING_MODE__");
 
   // C++ Standing Document Extensions.
   if (getLangOpts().CPlusPlus)
@@ -1654,6 +1655,30 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
   Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
   Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
 }
+  } else if (II == Ident__ROUNDING_MODE__) {
+switch (getCurrentRoundingMode()) {
+case LangOptions::RoundingMode::TowardZero:
+  OS << "_rtz";
+  break;
+case LangOptions::RoundingMode::NearestTiesToEven:
+  OS << "_rte";
+  break;
+case LangOptions::RoundingMode::TowardPositive:
+  OS << "_rtp";
+  break;
+case LangOptions::RoundingMode::TowardNegative:
+  OS << "_rtn";
+  break;
+case LangOptions::RoundingMode::NearestTiesToAway:
+  OS << "_rta";
+  break;
+ 

[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-20 Thread Serge Pavlov via cfe-commits

https://github.com/spavloff ready_for_review 
https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-20 Thread Serge Pavlov via cfe-commits


@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 -emit-llvm -triple i386-linux -Wno-unknown-pragmas %s -o - 
| FileCheck %s

spavloff wrote:

No, this macro is managed by the code in Sema, because pragma FENV_ROUND is 
processed there. If only `-E` is specified, __ROUNDING_MODE__ is always an 
empty string. We handle __FLT_EVAL_METHOD__ similarly.

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-19 Thread Eli Friedman via cfe-commits


@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 -emit-llvm -triple i386-linux -Wno-unknown-pragmas %s -o - 
| FileCheck %s

efriedma-quic wrote:

Since this is a preprocessor testcase, can you just use -E?

https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-19 Thread Serge Pavlov via cfe-commits

https://github.com/spavloff updated 
https://github.com/llvm/llvm-project/pull/92699

>From f8cd2539fb7f0388d7f3955f58b61b09da03bf0c Mon Sep 17 00:00:00 2001
From: Serge Pavlov 
Date: Sun, 19 May 2024 18:43:08 +0700
Subject: [PATCH] [clang] Macro for constant rounding mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The forthcoming C standard defines pragma FENV_ROUND to support constant
rounding mode. It also requires some functions to be evaluated with such
mode, N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be
implemented using preprocessor facility. Such implementation would
require a builtin macro that is set in the region where pragma
FENV_ROUND is in effect and reflects constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string
dependent on the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers.
Default value, when no pragma FENV_ROUND is specified, is empty string.
Concatenation of a function name with the builtin macro can be used to
obtain name of the function variant for particular rounding mode, like
"sin_rtz", or "__builtin_cos_rtd". The test "macro_rounding_mode.c"
added in this change provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also
depends on the results of semantic analysis.
---
 clang/include/clang/Lex/Preprocessor.h| 12 
 clang/lib/Lex/PPMacroExpansion.cpp| 25 +
 clang/lib/Sema/Sema.cpp   |  1 +
 clang/lib/Sema/SemaAttr.cpp   |  1 +
 clang/test/Preprocessor/macro_rounding_mode.c | 55 +++
 5 files changed, 94 insertions(+)
 create mode 100644 clang/test/Preprocessor/macro_rounding_mode.c

diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index c0850a8fa9f7f..295633b2e3c73 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -181,6 +181,7 @@ class Preprocessor {
   IdentifierInfo *Ident__is_target_variant_os;
   IdentifierInfo *Ident__is_target_variant_environment;
   IdentifierInfo *Ident__FLT_EVAL_METHOD__;// __FLT_EVAL_METHOD
+  IdentifierInfo *Ident__ROUNDING_MODE__;  // __ROUNDING_MODE__
 
   // Weak, only valid (and set) while InMacroArgs is true.
   Token* ArgMacro;
@@ -201,6 +202,9 @@ class Preprocessor {
   LangOptions::FPEvalMethodKind TUFPEvalMethod =
   LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
 
+  LangOptions::RoundingMode CurrentRoundingMode =
+  LangOptions::RoundingMode::Dynamic;
+
   // Next __COUNTER__ value, starts at 0.
   unsigned CounterValue = 0;
 
@@ -2356,6 +2360,14 @@ class Preprocessor {
 TUFPEvalMethod = Val;
   }
 
+  LangOptions::RoundingMode getCurrentRoundingMode() const {
+return CurrentRoundingMode;
+  }
+
+  void setCurrentRoundingMode(LangOptions::RoundingMode RM) {
+CurrentRoundingMode = RM;
+  }
+
   /// Retrieves the module that we're currently building, if any.
   Module *getCurrentModule();
 
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 8af4a97d00cb8..519fbfd666375 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -344,6 +344,7 @@ void Preprocessor::RegisterBuiltinMacros() {
   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
   Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, 
"__FLT_EVAL_METHOD__");
+  Ident__ROUNDING_MODE__ = RegisterBuiltinMacro(*this, "__ROUNDING_MODE__");
 
   // C++ Standing Document Extensions.
   if (getLangOpts().CPlusPlus)
@@ -1654,6 +1655,30 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
   Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
   Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
 }
+  } else if (II == Ident__ROUNDING_MODE__) {
+switch (getCurrentRoundingMode()) {
+case LangOptions::RoundingMode::TowardZero:
+  OS << "_rtz";
+  break;
+case LangOptions::RoundingMode::NearestTiesToEven:
+  OS 

[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-19 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 643f36184bd3d9a95cbfd608af6f169e0187 
15b6edbcc8fe4559e9a9d8930f88dbbb6ed38b8c -- 
clang/test/Preprocessor/macro_rounding_mode.c 
clang/include/clang/Lex/Preprocessor.h clang/lib/Lex/PPMacroExpansion.cpp 
clang/lib/Sema/Sema.cpp clang/lib/Sema/SemaAttr.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 519fbfd666..4186872533 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1799,8 +1799,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
 
 return false;
   });
-  } else if (II == Ident__has_cpp_attribute ||
- II == Ident__has_c_attribute) {
+  } else if (II == Ident__has_cpp_attribute || II == Ident__has_c_attribute) {
 bool IsCXX = II == Ident__has_cpp_attribute;
 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
 [&](Token &Tok, bool &HasLexedNextToken) -> int {
@@ -1830,8 +1829,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
getLangOpts())
 : 0;
 });
-  } else if (II == Ident__has_include ||
- II == Ident__has_include_next) {
+  } else if (II == Ident__has_include || II == Ident__has_include_next) {
 // The argument to these two builtins should be a parenthesized
 // file name string literal using angle brackets (<>) or
 // double-quotes ("").

``




https://github.com/llvm/llvm-project/pull/92699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Macro for constant rounding mode (PR #92699)

2024-05-19 Thread Serge Pavlov via cfe-commits

https://github.com/spavloff created 
https://github.com/llvm/llvm-project/pull/92699

The forthcoming C standard defines pragma FENV_ROUND to support constant 
rounding mode. It also requires some functions to be evaluated with such mode, 
N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be implemented 
using preprocessor facility. Such implementation would require a builtin macro 
that is set in the region where pragma FENV_ROUND is in effect and reflects 
constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string dependent on 
the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers. Default 
value, when no pragma FENV_ROUND is specified, is empty string. Concatenation 
of a function name with the builtin macro can be used to obtain name of the 
function variant for particular rounding mode, like "sin_rtz", or 
"__builtin_cos_rtd". The test "macro_rounding_mode.c" added in this change 
provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also depends 
on the results of semantic analysis.

>From 15b6edbcc8fe4559e9a9d8930f88dbbb6ed38b8c Mon Sep 17 00:00:00 2001
From: Serge Pavlov 
Date: Sun, 19 May 2024 18:43:08 +0700
Subject: [PATCH] [clang] Macro for constant rounding mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The forthcoming C standard defines pragma FENV_ROUND to support constant
rounding mode. It also requires some functions to be evaluated with such
mode, N3096 7.6.2p4 states:

Within the scope of an FENV_ROUND pragma establishing a mode other
than FE_DYNAMIC ... invocations of functions indicated in the table
below, for which macro replacement has not been suppressed (7.1.4),
shall be evaluated according to the specified constant rounding mode
... . Invocations of functions for which macro replacement has been
suppressed and invocations of functions other than those indicated
in the table below shall not be affected by constant rounding modes
– they are affected by (and affect) only the dynamic mode.

The way this requirement is formulated indicates that it could be
implemented using preprocessor facility. Such implementation would
require a builtin macro that is set in the region where pragma
FENV_ROUND is in effect and reflects constant rounding mode.

This change introduces macro __ROUNDING_MODE__, which is a string
dependent on the constant rounding mode:

FE_TOWARDZERO"_rtz"
FE_TONEAREST "_rte"
FE_DOWNWARD  "_rtp"
FE_UPWARD"_rtn"
FE_TONEARESTFROMZERO "_rta"
FE_DYNAMIC   empty string

All these values except "_rta" are OpenCL rounding mode modifiers.
Default value, when no pragma FENV_ROUND is specified, is empty string.
Concatenation of a function name with the builtin macro can be used to
obtain name of the function variant for particular rounding mode, like
"sin_rtz", or "__builtin_cos_rtd". The test "macro_rounding_mode.c"
added in this change provides an example of possible use.

The macro is implemented in the same way as FLT_EVAL_METHOD, which also
depends on the results of semantic analysis.
---
 clang/include/clang/Lex/Preprocessor.h| 12 
 clang/lib/Lex/PPMacroExpansion.cpp| 25 +
 clang/lib/Sema/Sema.cpp   |  1 +
 clang/lib/Sema/SemaAttr.cpp   |  1 +
 clang/test/Preprocessor/macro_rounding_mode.c | 55 +++
 5 files changed, 94 insertions(+)
 create mode 100644 clang/test/Preprocessor/macro_rounding_mode.c

diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index c0850a8fa9f7f..295633b2e3c73 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -181,6 +181,7 @@ class Preprocessor {
   IdentifierInfo *Ident__is_target_variant_os;
   IdentifierInfo *Ident__is_target_variant_environment;
   IdentifierInfo *Ident__FLT_EVAL_METHOD__;// __FLT_EVAL_METHOD
+  IdentifierInfo *Ident__ROUNDING_MODE__;  // __ROUNDING_MODE__
 
   // Weak, only valid (and set