[Mlir-commits] [mlir] [mlir][EmitC] lower roundeven to __builtin_roundeven (PR #227822)

Colin Cross llvmlistbot at llvm.org
Wed Sep 30 11:50:47 PDT 2026


https://github.com/colincross created https://github.com/llvm/llvm-project/pull/227822

std::roundeven doesn't exist, and roundeven was added in C23.  Lower roundeven ops to __builtin_roundeven instead.

Fixes #227522.

>From 99ad03c3be1c7c0910e24c39c815846ed19dfb92 Mon Sep 17 00:00:00 2001
From: Colin Cross <ccross at android.com>
Date: Tue, 29 Sep 2026 16:53:19 -0700
Subject: [PATCH] [mlir][EmitC] lower roundeven to __builtin_roundeven

std::roundeven doesn't exist, and roundeven was added in C23.  Lower
roundeven ops to __builtin_roundeven instead.
---
 mlir/lib/Conversion/MathToEmitC/MathToEmitC.cpp     | 4 ++--
 mlir/test/Conversion/MathToEmitC/math-to-emitc.mlir | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/mlir/lib/Conversion/MathToEmitC/MathToEmitC.cpp b/mlir/lib/Conversion/MathToEmitC/MathToEmitC.cpp
index adf06c139021f4..b2a52047822fb4 100644
--- a/mlir/lib/Conversion/MathToEmitC/MathToEmitC.cpp
+++ b/mlir/lib/Conversion/MathToEmitC/MathToEmitC.cpp
@@ -79,7 +79,7 @@ void mlir::registerConvertMathToEmitCInterface(DialectRegistry &registry) {
 }
 
 // Populates patterns to replace `math` operations with `emitc.call_opaque`,
-// using function names consistent with those in <math.h>.
+// using function names consistent with those in <math.h> or a builtin.
 void mlir::populateConvertMathToEmitCPatterns(
     RewritePatternSet &patterns, emitc::LanguageTarget languageTarget) {
   auto *context = patterns.getContext();
@@ -88,7 +88,7 @@ void mlir::populateConvertMathToEmitCPatterns(
   patterns.insert<LowerToEmitCCallOpaque<math::RoundOp>>(context, "round",
                                                          languageTarget);
   patterns.insert<LowerToEmitCCallOpaque<math::RoundEvenOp>>(
-      context, "roundeven", languageTarget);
+      context, "__builtin_roundeven", emitc::LanguageTarget::c99);
   patterns.insert<LowerToEmitCCallOpaque<math::ExpOp>>(context, "exp",
                                                        languageTarget);
   patterns.insert<LowerToEmitCCallOpaque<math::CosOp>>(context, "cos",
diff --git a/mlir/test/Conversion/MathToEmitC/math-to-emitc.mlir b/mlir/test/Conversion/MathToEmitC/math-to-emitc.mlir
index 0947556e20fb53..3d7d8ac5743f74 100644
--- a/mlir/test/Conversion/MathToEmitC/math-to-emitc.mlir
+++ b/mlir/test/Conversion/MathToEmitC/math-to-emitc.mlir
@@ -124,10 +124,10 @@ func.func @round(%arg0: f32, %arg1: f64) {
 }
 
 func.func @roundeven(%arg0: f32, %arg1: f64) {
-  // c99: emitc.call_opaque "roundevenf"
-  // c99-NEXT: emitc.call_opaque "roundeven"
-  // cpp11: emitc.call_opaque "std::roundeven"
-  // cpp11-NEXT: emitc.call_opaque "std::roundeven"
+  // c99: emitc.call_opaque "__builtin_roundevenf"
+  // c99-NEXT: emitc.call_opaque "__builtin_roundeven"
+  // cpp11: emitc.call_opaque "__builtin_roundevenf"
+  // cpp11-NEXT: emitc.call_opaque "__builtin_roundeven"
   %0 = math.roundeven %arg0 : f32
   %1 = math.roundeven %arg1 : f64
   return



More information about the Mlir-commits mailing list