Skip to content

Commit

Permalink
[MLIR] Remove dependency on omp dialect in LLVM dialect.
Browse files Browse the repository at this point in the history
This fixes a buildbot failure where the dependency on the omp dialect
in the LLVM dialect caused error. Instead of accessing the interface
defined in the omp dialect we directly access the attributes
instead. To make this work the IsDeviceAttr is removed and replaced
with a BoolAttr instead.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D151745
  • Loading branch information
jsjodin committed May 31, 2023
1 parent e5399f1 commit f6a631d
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion flang/test/Fir/omp-declare-target-data.fir
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s

module attributes {omp.is_device = #omp.isdevice<is_device = false>} {
module attributes {omp.is_device = false} {

// CHECK: llvm.mlir.global external @_QMtest_0Earray_1d(dense<[1, 2, 3]> : tensor<3xi32>) {{{.*}}omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link)>{{.*}}} : !llvm.array<3 x i32>
fir.global @_QMtest_0Earray_1d(dense<[1, 2, 3]> : tensor<3xi32>) {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link)>} : !fir.array<3xi32>
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Lower/OpenMP/omp-host-ir-flag.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s 2>&1
!RUN: %flang_fc1 -emit-mlir -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s

!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = #omp.isdevice<is_device = true>{{.*}}}
!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = true{{.*}}}
subroutine omp_subroutine()
end subroutine omp_subroutine
6 changes: 3 additions & 3 deletions flang/test/Lower/OpenMP/omp-is-device.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
!RUN: bbc -fopenmp -emit-fir -o - %s | FileCheck %s --check-prefix=HOST
!RUN: bbc -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY

!DEVICE: module attributes {{{.*}}, omp.is_device = #omp.isdevice<is_device = true>{{.*}}}
!HOST: module attributes {{{.*}}, omp.is_device = #omp.isdevice<is_device = false>{{.*}}}
!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
!DEVICE-FLAG-ONLY: module attributes {{{.*}}"
!DEVICE-FLAG-ONLY-NOT: , omp.is_device = #omp.isdevice<{{.*}}>
!DEVICE-FLAG-ONLY-NOT: , omp.is_device = {{.*}}
!DEVICE-FLAG-ONLY-SAME: }
subroutine omp_subroutine()
end subroutine omp_subroutine
8 changes: 4 additions & 4 deletions flang/test/Lower/OpenMP/rtl-flags.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
!RUN: bbc -emit-fir -fopenmp -fopenmp-assume-no-nested-parallelism -fopenmp-is-device -o - %s | FileCheck %s --check-prefix=NEST-PAR-DEVICE-FIR
!RUN: bbc -emit-fir -fopenmp -fopenmp-target-debug=1 -fopenmp-assume-teams-oversubscription -fopenmp-assume-no-nested-parallelism -fopenmp-assume-threads-oversubscription -fopenmp-assume-no-thread-state -fopenmp-is-device -o - %s | FileCheck %s --check-prefix=ALL-DEVICE-FIR

!DEFAULT-DEVICE-FIR: module attributes {{{.*}}, omp.flags = #omp.flags<openmp_device_version = 11>, omp.is_device = #omp.isdevice<is_device = true>{{.*}}}
!DEFAULT-DEVICE-FIR-VERSION: module attributes {{{.*}}, omp.flags = #omp.flags<openmp_device_version = 45>, omp.is_device = #omp.isdevice<is_device = true>, omp.version = #omp.version<version = 45>{{.*}}
!DEFAULT-HOST-FIR: module attributes {{{.*}}, omp.is_device = #omp.isdevice<is_device = false>{{.*}}
!DEFAULT-HOST-FIR-VERSION: module attributes {{{.*}}, omp.is_device = #omp.isdevice<is_device = false>, omp.version = #omp.version<version = 45>{{.*}}
!DEFAULT-DEVICE-FIR: module attributes {{{.*}}, omp.flags = #omp.flags<openmp_device_version = 11>, omp.is_device = true{{.*}}}
!DEFAULT-DEVICE-FIR-VERSION: module attributes {{{.*}}, omp.flags = #omp.flags<openmp_device_version = 45>, omp.is_device = true, omp.version = #omp.version<version = 45>{{.*}}
!DEFAULT-HOST-FIR: module attributes {{{.*}}, omp.is_device = false{{.*}}
!DEFAULT-HOST-FIR-VERSION: module attributes {{{.*}}, omp.is_device = false, omp.version = #omp.version<version = 45>{{.*}}
!DBG-DEVICE-FIR: module attributes {{{.*}}, omp.flags = #omp.flags<debug_kind = 1, openmp_device_version = 11>{{.*}}}
!DBG-EQ-DEVICE-FIR: module attributes {{{.*}}, omp.flags = #omp.flags<debug_kind = 111, openmp_device_version = 11>{{.*}}}
!TEAMS-OSUB-DEVICE-FIR: module attributes {{{.*}}, omp.flags = #omp.flags<assume_teams_oversubscription = true, openmp_device_version = 11>{{.*}}}
Expand Down
8 changes: 0 additions & 8 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ class OpenMP_Attr<string name, string attrMnemonic,
let mnemonic = attrMnemonic;
}

def IsDeviceAttr : OpenMP_Attr<"IsDevice", "isdevice"> {
let parameters = (ins
"bool":$is_device
);

let assemblyFormat = "`<` struct(params) `>`";
}

def VersionAttr : OpenMP_Attr<"Version", "version"> {
let parameters = (ins
"uint32_t":$version
Expand Down
14 changes: 7 additions & 7 deletions mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,27 @@ def OffloadModuleInterface : OpInterface<"OffloadModuleInterface"> {
let methods = [
InterfaceMethod<
/*description=*/[{
Set the attribute IsDeviceAttr on the current module with the
specified boolean argument.
Set the attribute on the current module with the specified boolean
argument.
}],
/*retTy=*/"void",
/*methodName=*/"setIsDevice",
(ins "bool":$isDevice), [{}], [{
$_op->setAttr(
mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.is_device"}),
mlir::omp::IsDeviceAttr::get($_op->getContext(), isDevice));
mlir::BoolAttr::get($_op->getContext(), isDevice));
}]>,
InterfaceMethod<
/*description=*/[{
Get the IsDeviceAttr attribute on the current module if it exists and return
its value, if it doesn't exist it returns false by default.
Get the attribute on the current module if it exists and
return its value, if it doesn't exist it returns false by default.
}],
/*retTy=*/"bool",
/*methodName=*/"getIsDevice",
(ins), [{}], [{
if (Attribute isDevice = $_op->getAttr("omp.is_device"))
if (::llvm::isa<mlir::omp::IsDeviceAttr>(isDevice))
return ::llvm::dyn_cast<IsDeviceAttr>(isDevice).getIsDevice();
if (isDevice.isa<mlir::BoolAttr>())
return isDevice.dyn_cast<BoolAttr>().getValue();
return false;
}]>,
InterfaceMethod<
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Target/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ add_mlir_translation_library(MLIRTargetLLVMIRExport
MLIRLLVMDialect
MLIRLLVMIRTransforms
MLIRTranslateLib
MLIROpenMPDialect
)

add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
Expand Down
14 changes: 9 additions & 5 deletions mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,11 +1276,15 @@ llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {

bool isDevice = false;
llvm::StringRef hostIRFilePath = "";
if (auto offloadMod =
dyn_cast<mlir::omp::OffloadModuleInterface>(mlirModule)) {
isDevice = offloadMod.getIsDevice();
hostIRFilePath = offloadMod.getHostIRFilePath();
}

if (Attribute deviceAttr = mlirModule->getAttr("omp.is_device"))
if (::llvm::isa<mlir::BoolAttr>(deviceAttr))
isDevice = ::llvm::dyn_cast<mlir::BoolAttr>(deviceAttr).getValue();

if (Attribute filepath = mlirModule->getAttr("omp.host_ir_filepath"))
if (::llvm::isa<mlir::StringAttr>(filepath))
hostIRFilePath =
::llvm::dyn_cast<mlir::StringAttr>(filepath).getValue();

ompBuilder->initialize(hostIRFilePath);

Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Target/LLVMIR/omptarget-region-device-llvm.mlir
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s

module attributes {omp.is_device = #omp.isdevice<is_device = true>} {
module attributes {omp.is_device = true} {
llvm.func @omp_target_region_() {
%0 = llvm.mlir.constant(20 : i32) : i32
%1 = llvm.mlir.constant(10 : i32) : i32
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Target/LLVMIR/omptarget-region-llvm.mlir
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s

module attributes {omp.is_device = #omp.isdevice<is_device = false>} {
module attributes {omp.is_device = false} {
llvm.func @omp_target_region_() {
%0 = llvm.mlir.constant(20 : i32) : i32
%1 = llvm.mlir.constant(10 : i32) : i32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s

module attributes {omp.is_device = #omp.isdevice<is_device = false>} {
module attributes {omp.is_device = false} {
llvm.func @omp_target_region_() {
%0 = llvm.mlir.constant(20 : i32) : i32
%1 = llvm.mlir.constant(10 : i32) : i32
Expand Down

0 comments on commit f6a631d

Please sign in to comment.