Skip to content

Commit d40285e

Browse files
authored
Fix MSVC build by defining _USE_MATH_DEFINES earlier. (#2576)
The fix in #2254 is not sufficient because the first file that includes `<cmath>` is what determines which macros are defined. Downstream Windows builds broke at some point due to this: https://github.com/iree-org/iree/actions/runs/11159458848/job/31017884277#step:7:6258 ``` [5387/8614] Building CXX object llvm-external-projects\stablehlo\stablehlo\transforms\CMakeFiles\obj.StablehloPasses.dir\ChloLegalizeToStablehlo.cpp.obj FAILED: llvm-external-projects/stablehlo/stablehlo/transforms/CMakeFiles/obj.StablehloPasses.dir/ChloLegalizeToStablehlo.cpp.obj C:\ProgramData\Chocolatey\bin\ccache C:\PROGRA~1\MICROS~2\2022\ENTERP~1\VC\Tools\MSVC\1441~1.341\bin\Hostx64\x64\cl.exe /nologo /TP -DGTEST_HAS_RTTI=0 -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -ID:\a\iree\iree\third_party\llvm-project\llvm\include -ID:\a\iree\iree\build-windows\llvm-project\include -ID:\a\iree\iree\third_party\llvm-project\mlir\include -ID:\a\iree\iree\third_party\stablehlo -ID:\a\iree\iree\build-windows\llvm-external-projects\stablehlo -ID:\a\iree\iree\third_party\llvm-project\lld\include -ID:\a\iree\iree\build-windows\llvm-project\tools\lld\include -external:I\..\mlir\include -external:ID:\a\iree\iree\build-windows\llvm-project\tools\mlir\include -external:W0 /DWIN32 /D_WINDOWS /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /Z7 /O2 /Ob1 -std:c++17 -MD /EHs-c- /GR- /showIncludes /Follvm-external-projects\stablehlo\stablehlo\transforms\CMakeFiles\obj.StablehloPasses.dir\ChloLegalizeToStablehlo.cpp.obj /Fdllvm-external-projects\stablehlo\stablehlo\transforms\CMakeFiles\obj.StablehloPasses.dir\ /FS -c D:\a\iree\iree\third_party\stablehlo\stablehlo\transforms\ChloLegalizeToStablehlo.cpp D:\a\iree\iree\third_party\stablehlo\stablehlo\transforms\ChloLegalizeToStablehlo.cpp(1324): error C2065: 'M_PI': undeclared identifier D:\a\iree\iree\third_party\stablehlo\stablehlo\transforms\ChloLegalizeToStablehlo.cpp(1324): error C2660: 'mlir::stablehlo::getConstantLike': function does not take 3 arguments D:\a\iree\iree\third_party\stablehlo\stablehlo/transforms/PassUtils.h(51): note: see declaration of 'mlir::stablehlo::getConstantLike' ```
1 parent 5f3c287 commit d40285e

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ if(LLVM_ENABLE_ZLIB)
128128
find_package(ZLIB)
129129
endif()
130130

131+
#-------------------------------------------------------------------------------
132+
# Compiler options
133+
#-------------------------------------------------------------------------------
134+
135+
if(MSVC)
136+
# Required to use M_PI. Must be set before any `#include <cmath>`.
137+
add_compile_definitions("_USE_MATH_DEFINES")
138+
endif()
139+
131140
#-------------------------------------------------------------------------------
132141
# Performance configuration
133142
#-------------------------------------------------------------------------------

stablehlo/transforms/ChloLegalizeToStablehlo.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <algorithm>
1010
#include <array>
1111
#include <cassert>
12+
#include <cmath>
1213
#include <cstddef>
1314
#include <cstdint>
1415
#include <limits>
@@ -18,33 +19,30 @@
1819

1920
#include "llvm/ADT/APFloat.h"
2021
#include "llvm/ADT/ArrayRef.h"
22+
#include "llvm/ADT/STLExtras.h"
2123
#include "llvm/ADT/Sequence.h"
2224
#include "llvm/ADT/SmallVector.h"
2325
#include "llvm/Support/ErrorHandling.h"
2426
#include "mlir/Dialect/Arith/IR/Arith.h"
27+
#include "mlir/Dialect/Complex/IR/Complex.h"
2528
#include "mlir/Dialect/Func/IR/FuncOps.h"
29+
#include "mlir/Dialect/Shape/IR/Shape.h"
30+
#include "mlir/Dialect/Tensor/IR/Tensor.h"
2631
#include "mlir/IR/Builders.h"
2732
#include "mlir/IR/BuiltinAttributeInterfaces.h"
2833
#include "mlir/IR/BuiltinAttributes.h"
2934
#include "mlir/IR/BuiltinTypeInterfaces.h"
30-
#include "mlir/IR/ValueRange.h"
31-
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
32-
#include "mlir/Support/LLVM.h"
33-
#include "stablehlo/dialect/Base.h"
34-
#define _USE_MATH_DEFINES
35-
#include <cmath>
36-
37-
#include "llvm/ADT/STLExtras.h"
38-
#include "mlir/Dialect/Complex/IR/Complex.h"
39-
#include "mlir/Dialect/Shape/IR/Shape.h"
40-
#include "mlir/Dialect/Tensor/IR/Tensor.h"
4135
#include "mlir/IR/BuiltinTypes.h"
4236
#include "mlir/IR/ImplicitLocOpBuilder.h"
4337
#include "mlir/IR/MLIRContext.h"
4438
#include "mlir/IR/PatternMatch.h"
4539
#include "mlir/IR/TypeUtilities.h"
40+
#include "mlir/IR/ValueRange.h"
41+
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
42+
#include "mlir/Support/LLVM.h"
4643
#include "mlir/Support/LogicalResult.h"
4744
#include "mlir/Transforms/DialectConversion.h"
45+
#include "stablehlo/dialect/Base.h"
4846
#include "stablehlo/dialect/BroadcastUtils.h"
4947
#include "stablehlo/dialect/ChloOps.h"
5048
#include "stablehlo/dialect/StablehloOps.h"

0 commit comments

Comments
 (0)