Skip to content

[MLIR][ROCDL] Add conversion of math.erfc to AMD GPU library calls #128899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

jsjodin
Copy link
Contributor

@jsjodin jsjodin commented Feb 26, 2025

This patch adds a pattern to convert the math.erfc operation to AMD GPU library calls.

Depends on: #128897 for the flang test

@jsjodin jsjodin requested review from arsenm and krzysz00 February 26, 2025 16:05
@jsjodin jsjodin marked this pull request as ready for review February 26, 2025 16:07
@llvmbot llvmbot added backend:AMDGPU mlir flang Flang issues not falling into any other category flang:fir-hlfir flang:openmp labels Feb 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2025

@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-flang-fir-hlfir

Author: Jan Leyonberg (jsjodin)

Changes

This patch adds a pattern to convert the math.erfc operation to AMD GPU library calls.

Depends on: #128897 for the flang test


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

3 Files Affected:

  • (modified) flang/test/Lower/OpenMP/math-amdgpu.f90 (+14)
  • (modified) mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp (+2)
  • (modified) mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir (+18)
diff --git a/flang/test/Lower/OpenMP/math-amdgpu.f90 b/flang/test/Lower/OpenMP/math-amdgpu.f90
index 116768ba9412a..55a14f7d31378 100644
--- a/flang/test/Lower/OpenMP/math-amdgpu.f90
+++ b/flang/test/Lower/OpenMP/math-amdgpu.f90
@@ -99,6 +99,20 @@ subroutine omp_erf_f64(x, y)
   y = erf(x)
 end subroutine omp_erf_f64
 
+subroutine omp_erfc_f32(x, y)
+!$omp declare target
+  real :: x, y
+!CHECK: call float @__ocml_erfc_f32(float {{.*}})
+  y = erfc(x)
+end subroutine omp_erfc_f32
+
+subroutine omp_erfc_f64(x, y)
+!$omp declare target
+  real(8) :: x, y
+!CHECK: call double @__ocml_erfc_f64(double {{.*}})
+  y = erfc(x)
+end subroutine omp_erfc_f64
+
 subroutine omp_exp_f32(x, y)
 !$omp declare target
   real :: x, y
diff --git a/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp b/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
index 838eef30a938f..e065c804507ac 100644
--- a/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
+++ b/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
@@ -113,6 +113,8 @@ void mlir::populateMathToROCDLConversionPatterns(
                                   "__ocml_tan_f64", "__ocml_tan_f16");
   populateOpPatterns<math::ErfOp>(converter, patterns, "__ocml_erf_f32",
                                   "__ocml_erf_f64", "__ocml_erf_f16");
+  populateOpPatterns<math::ErfcOp>(converter, patterns, "__ocml_erfc_f32",
+                                   "__ocml_erfc_f64", "__ocml_erfc_f16");
   populateOpPatterns<math::FPowIOp>(converter, patterns, "__ocml_pown_f32",
                                     "__ocml_pown_f64", "__ocml_pown_f16");
   // Single arith pattern that needs a ROCDL call, probably not
diff --git a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
index 313d7b086731e..dbff23339d8b3 100644
--- a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
+++ b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
@@ -462,6 +462,24 @@ module @test_module {
 
 // -----
 
+module @test_module {
+  // CHECK: llvm.func @__ocml_erfc_f16(f16) -> f16
+  // CHECK: llvm.func @__ocml_erfc_f32(f32) -> f32
+  // CHECK: llvm.func @__ocml_erfc_f64(f64) -> f64
+  // CHECK-LABEL: func @math_erfc
+  func.func @math_erfc(%arg_f16 : f16, %arg_f32 : f32, %arg_f64 : f64) -> (f16, f32, f64) {
+    %result16 = math.erfc %arg_f16 : f16
+    // CHECK: llvm.call @__ocml_erfc_f16(%{{.*}}) : (f16) -> f16
+    %result32 = math.erfc %arg_f32 : f32
+    // CHECK: llvm.call @__ocml_erfc_f32(%{{.*}}) : (f32) -> f32
+    %result64 = math.erfc %arg_f64 : f64
+    // CHECK: llvm.call @__ocml_erfc_f64(%{{.*}}) : (f64) -> f64
+    func.return %result16, %result32, %result64 : f16, f32, f64
+  }
+}
+
+// -----
+
 module @test_module {
   // CHECK: llvm.func @__ocml_sin_f16(f16) -> f16
   // CHECK: llvm.func @__ocml_sin_f32(f32) -> f32

@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2025

@llvm/pr-subscribers-mlir

Author: Jan Leyonberg (jsjodin)

Changes

This patch adds a pattern to convert the math.erfc operation to AMD GPU library calls.

Depends on: #128897 for the flang test


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

3 Files Affected:

  • (modified) flang/test/Lower/OpenMP/math-amdgpu.f90 (+14)
  • (modified) mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp (+2)
  • (modified) mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir (+18)
diff --git a/flang/test/Lower/OpenMP/math-amdgpu.f90 b/flang/test/Lower/OpenMP/math-amdgpu.f90
index 116768ba9412a..55a14f7d31378 100644
--- a/flang/test/Lower/OpenMP/math-amdgpu.f90
+++ b/flang/test/Lower/OpenMP/math-amdgpu.f90
@@ -99,6 +99,20 @@ subroutine omp_erf_f64(x, y)
   y = erf(x)
 end subroutine omp_erf_f64
 
+subroutine omp_erfc_f32(x, y)
+!$omp declare target
+  real :: x, y
+!CHECK: call float @__ocml_erfc_f32(float {{.*}})
+  y = erfc(x)
+end subroutine omp_erfc_f32
+
+subroutine omp_erfc_f64(x, y)
+!$omp declare target
+  real(8) :: x, y
+!CHECK: call double @__ocml_erfc_f64(double {{.*}})
+  y = erfc(x)
+end subroutine omp_erfc_f64
+
 subroutine omp_exp_f32(x, y)
 !$omp declare target
   real :: x, y
diff --git a/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp b/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
index 838eef30a938f..e065c804507ac 100644
--- a/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
+++ b/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
@@ -113,6 +113,8 @@ void mlir::populateMathToROCDLConversionPatterns(
                                   "__ocml_tan_f64", "__ocml_tan_f16");
   populateOpPatterns<math::ErfOp>(converter, patterns, "__ocml_erf_f32",
                                   "__ocml_erf_f64", "__ocml_erf_f16");
+  populateOpPatterns<math::ErfcOp>(converter, patterns, "__ocml_erfc_f32",
+                                   "__ocml_erfc_f64", "__ocml_erfc_f16");
   populateOpPatterns<math::FPowIOp>(converter, patterns, "__ocml_pown_f32",
                                     "__ocml_pown_f64", "__ocml_pown_f16");
   // Single arith pattern that needs a ROCDL call, probably not
diff --git a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
index 313d7b086731e..dbff23339d8b3 100644
--- a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
+++ b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
@@ -462,6 +462,24 @@ module @test_module {
 
 // -----
 
+module @test_module {
+  // CHECK: llvm.func @__ocml_erfc_f16(f16) -> f16
+  // CHECK: llvm.func @__ocml_erfc_f32(f32) -> f32
+  // CHECK: llvm.func @__ocml_erfc_f64(f64) -> f64
+  // CHECK-LABEL: func @math_erfc
+  func.func @math_erfc(%arg_f16 : f16, %arg_f32 : f32, %arg_f64 : f64) -> (f16, f32, f64) {
+    %result16 = math.erfc %arg_f16 : f16
+    // CHECK: llvm.call @__ocml_erfc_f16(%{{.*}}) : (f16) -> f16
+    %result32 = math.erfc %arg_f32 : f32
+    // CHECK: llvm.call @__ocml_erfc_f32(%{{.*}}) : (f32) -> f32
+    %result64 = math.erfc %arg_f64 : f64
+    // CHECK: llvm.call @__ocml_erfc_f64(%{{.*}}) : (f64) -> f64
+    func.return %result16, %result32, %result64 : f16, f32, f64
+  }
+}
+
+// -----
+
 module @test_module {
   // CHECK: llvm.func @__ocml_sin_f16(f16) -> f16
   // CHECK: llvm.func @__ocml_sin_f32(f32) -> f32

@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2025

@llvm/pr-subscribers-flang-openmp

Author: Jan Leyonberg (jsjodin)

Changes

This patch adds a pattern to convert the math.erfc operation to AMD GPU library calls.

Depends on: #128897 for the flang test


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

3 Files Affected:

  • (modified) flang/test/Lower/OpenMP/math-amdgpu.f90 (+14)
  • (modified) mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp (+2)
  • (modified) mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir (+18)
diff --git a/flang/test/Lower/OpenMP/math-amdgpu.f90 b/flang/test/Lower/OpenMP/math-amdgpu.f90
index 116768ba9412a..55a14f7d31378 100644
--- a/flang/test/Lower/OpenMP/math-amdgpu.f90
+++ b/flang/test/Lower/OpenMP/math-amdgpu.f90
@@ -99,6 +99,20 @@ subroutine omp_erf_f64(x, y)
   y = erf(x)
 end subroutine omp_erf_f64
 
+subroutine omp_erfc_f32(x, y)
+!$omp declare target
+  real :: x, y
+!CHECK: call float @__ocml_erfc_f32(float {{.*}})
+  y = erfc(x)
+end subroutine omp_erfc_f32
+
+subroutine omp_erfc_f64(x, y)
+!$omp declare target
+  real(8) :: x, y
+!CHECK: call double @__ocml_erfc_f64(double {{.*}})
+  y = erfc(x)
+end subroutine omp_erfc_f64
+
 subroutine omp_exp_f32(x, y)
 !$omp declare target
   real :: x, y
diff --git a/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp b/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
index 838eef30a938f..e065c804507ac 100644
--- a/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
+++ b/mlir/lib/Conversion/MathToROCDL/MathToROCDL.cpp
@@ -113,6 +113,8 @@ void mlir::populateMathToROCDLConversionPatterns(
                                   "__ocml_tan_f64", "__ocml_tan_f16");
   populateOpPatterns<math::ErfOp>(converter, patterns, "__ocml_erf_f32",
                                   "__ocml_erf_f64", "__ocml_erf_f16");
+  populateOpPatterns<math::ErfcOp>(converter, patterns, "__ocml_erfc_f32",
+                                   "__ocml_erfc_f64", "__ocml_erfc_f16");
   populateOpPatterns<math::FPowIOp>(converter, patterns, "__ocml_pown_f32",
                                     "__ocml_pown_f64", "__ocml_pown_f16");
   // Single arith pattern that needs a ROCDL call, probably not
diff --git a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
index 313d7b086731e..dbff23339d8b3 100644
--- a/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
+++ b/mlir/test/Conversion/MathToROCDL/math-to-rocdl.mlir
@@ -462,6 +462,24 @@ module @test_module {
 
 // -----
 
+module @test_module {
+  // CHECK: llvm.func @__ocml_erfc_f16(f16) -> f16
+  // CHECK: llvm.func @__ocml_erfc_f32(f32) -> f32
+  // CHECK: llvm.func @__ocml_erfc_f64(f64) -> f64
+  // CHECK-LABEL: func @math_erfc
+  func.func @math_erfc(%arg_f16 : f16, %arg_f32 : f32, %arg_f64 : f64) -> (f16, f32, f64) {
+    %result16 = math.erfc %arg_f16 : f16
+    // CHECK: llvm.call @__ocml_erfc_f16(%{{.*}}) : (f16) -> f16
+    %result32 = math.erfc %arg_f32 : f32
+    // CHECK: llvm.call @__ocml_erfc_f32(%{{.*}}) : (f32) -> f32
+    %result64 = math.erfc %arg_f64 : f64
+    // CHECK: llvm.call @__ocml_erfc_f64(%{{.*}}) : (f64) -> f64
+    func.return %result16, %result32, %result64 : f16, f32, f64
+  }
+}
+
+// -----
+
 module @test_module {
   // CHECK: llvm.func @__ocml_sin_f16(f16) -> f16
   // CHECK: llvm.func @__ocml_sin_f32(f32) -> f32

Copy link
Contributor

@krzysz00 krzysz00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, approved, thank you

@jsjodin jsjodin force-pushed the jleyonberg/rocdlerfc branch from f8d8b4d to 5ee67e6 Compare February 27, 2025 16:59
This patch adds a pattern to convert the math.erfc operation to AMD GPU
library calls.
@jsjodin jsjodin force-pushed the jleyonberg/rocdlerfc branch from 5ee67e6 to d6d4851 Compare February 27, 2025 17:54
@jsjodin jsjodin merged commit c3b3352 into llvm:main Feb 27, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AMDGPU flang:fir-hlfir flang:openmp flang Flang issues not falling into any other category mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants