[flang-commits] [PATCH] D135853: [flang] Rename hypotf on MSVC platforms

Diana Picus via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Oct 20 01:39:48 PDT 2022


rovka updated this revision to Diff 469133.
rovka added a comment.

Address review comments. 
Thanks for the feedback :)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135853/new/

https://reviews.llvm.org/D135853

Files:
  flang/lib/Optimizer/CodeGen/CodeGen.cpp
  flang/test/Fir/rename-msvc-libm.fir


Index: flang/test/Fir/rename-msvc-libm.fir
===================================================================
--- /dev/null
+++ flang/test/Fir/rename-msvc-libm.fir
@@ -0,0 +1,17 @@
+// RUN: fir-opt --fir-to-llvm-ir="target=aarch64-unknown-linux-gnu" %s | FileCheck %s -DHYPOTF=hypotf
+// RUN: fir-opt --fir-to-llvm-ir="target=aarch64-pc-windows-msvc" %s | FileCheck %s -DHYPOTF=_hypotf
+
+// Test hypotf renaming
+
+func.func private @hypotf(f32, f32) -> f32
+
+// CHECK: llvm.func @[[HYPOTF]](f32, f32) -> f32
+
+func.func @call_hypotf(%arg0 : f32, %arg1 : f32) -> f32 {
+  %0 = fir.call @hypotf(%arg0, %arg1) : (f32, f32) -> f32
+  return %0 : f32
+}
+
+// CHECK-LABEL: llvm.func @call_hypotf
+// CHECK-SAME: %[[arg0:.*]]: f32, %[[arg1:.*]]: f32
+// CHECK: llvm.call @[[HYPOTF]](%[[arg0]], %[[arg1]]) : (f32, f32) -> f32
Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -3447,6 +3447,43 @@
 
 } // namespace
 
+namespace {
+class RenameMSVCLibmCallees
+    : public mlir::OpRewritePattern<mlir::LLVM::CallOp> {
+public:
+  using OpRewritePattern::OpRewritePattern;
+
+  mlir::LogicalResult
+  matchAndRewrite(mlir::LLVM::CallOp op,
+                  mlir::PatternRewriter &rewriter) const override {
+    rewriter.startRootUpdate(op);
+    auto callee = op.getCallee();
+    if (callee)
+      if (callee->equals("hypotf"))
+        op.setCalleeAttr(mlir::SymbolRefAttr::get(op.getContext(), "_hypotf"));
+
+    rewriter.finalizeRootUpdate(op);
+    return mlir::success();
+  }
+};
+
+class RenameMSVCLibmFuncs
+    : public mlir::OpRewritePattern<mlir::LLVM::LLVMFuncOp> {
+public:
+  using OpRewritePattern::OpRewritePattern;
+
+  mlir::LogicalResult
+  matchAndRewrite(mlir::LLVM::LLVMFuncOp op,
+                  mlir::PatternRewriter &rewriter) const override {
+    rewriter.startRootUpdate(op);
+    if (op.getSymName().equals("hypotf"))
+      op.setSymNameAttr(rewriter.getStringAttr("_hypotf"));
+    rewriter.finalizeRootUpdate(op);
+    return mlir::success();
+  }
+};
+} // namespace
+
 namespace {
 /// Convert FIR dialect to LLVM dialect
 ///
@@ -3549,6 +3586,24 @@
     // required NOPs for applying a full conversion
     target.addLegalOp<mlir::ModuleOp>();
 
+    // If we're on Windows, we might need to rename some libm calls.
+    bool isMSVC = fir::getTargetTriple(mod).isOSMSVCRT();
+    if (isMSVC) {
+      pattern.insert<RenameMSVCLibmCallees, RenameMSVCLibmFuncs>(context);
+
+      target.addDynamicallyLegalOp<mlir::LLVM::CallOp>(
+          [](mlir::LLVM::CallOp op) {
+            auto callee = op.getCallee();
+            if (!callee)
+              return true;
+            return !callee->equals("hypotf");
+          });
+      target.addDynamicallyLegalOp<mlir::LLVM::LLVMFuncOp>(
+          [](mlir::LLVM::LLVMFuncOp op) {
+            return !op.getSymName().equals("hypotf");
+          });
+    }
+
     // apply the patterns
     if (mlir::failed(mlir::applyFullConversion(getModule(), target,
                                                std::move(pattern)))) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135853.469133.patch
Type: text/x-patch
Size: 3174 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221020/515e0db9/attachment-0001.bin>


More information about the flang-commits mailing list