Skip to content

Commit cc3d116

Browse files
whitneywhtsangetiotto
authored andcommitted
Add type converter from sycl (id and range) type to llvm type (#34)
Type conversions are still work in progress. This PR adds the support of type converters of `sycl::IDType` and `sycl::RangeType` to LLVM types. Signed-off-by: Tsang, Whitney <whitney.tsang@intel.com>
1 parent c762f29 commit cc3d116

File tree

7 files changed

+134
-1
lines changed

7 files changed

+134
-1
lines changed

mlir-sycl/include/SYCL/SYCLToLLVM.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) Intel
2+
3+
//===--- SYCLToLLVM.h -----------------------------------------------------===//
4+
//
5+
// MLIR-SYCL is under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef SYCL_SYCLTOLLVM_H_
12+
#define SYCL_SYCLTOLLVM_H_
13+
14+
namespace mlir {
15+
class LLVMTypeConverter;
16+
class RewritePatternSet;
17+
18+
namespace sycl {
19+
/// Collect the patterns to convert from the SYCL dialect to LLVM dialect. The
20+
/// conversion patterns capture the LLVMTypeConverter by reference meaning the
21+
/// references have to remain alive during the entire pattern lifetime.
22+
void populateSYCLToLLVMConversionPatterns(LLVMTypeConverter &converter,
23+
RewritePatternSet &patterns);
24+
} // namespace sycl
25+
} // namespace mlir
26+
27+
#endif // SYCL_SYCLTOLLVM_H_

mlir-sycl/lib/SYCL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ add_mlir_dialect_library(MLIRSYCLDialect
3939
SYCLOpsDialect.cpp
4040
SYCLOpsTypes.cpp
4141
SYCLOpsAlias.cpp
42+
SYCLToLLVM.cpp
4243

4344
ADDITIONAL_HEADER_DIRS
4445
${PROJECT_SOURCE_DIR}/include/SYCL

mlir-sycl/lib/SYCL/SYCLToLLVM.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (C) Intel
2+
3+
//===--- SYCLToLLVM.cpp ---------------------------------------------------===//
4+
//
5+
// MLIR-SYCL is under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This file implements a pass to convert SYCL dialects types to their
12+
// corresponding LLVM dialect types.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#include "SYCL/SYCLToLLVM.h"
17+
#include "SYCL/SYCLOpsTypes.h"
18+
#include "mlir/Conversion/LLVMCommon/Pattern.h"
19+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
20+
21+
using namespace mlir;
22+
23+
namespace {
24+
// Get the LLVM type of "class.cl::sycl::detail::array" with number of
25+
// dimentions \p dimNum and element type \p type.
26+
static Type getSYCLArrayTy(MLIRContext &context, unsigned dimNum, Type type) {
27+
assert((dimNum == 1 || dimNum == 2 || dimNum == 3) &&
28+
"Expecting number of dimensions to be 1, 2, or 3.");
29+
auto structTy = LLVM::LLVMStructType::getIdentified(
30+
&context, "class.cl::sycl::detail::array." + std::to_string(dimNum));
31+
if (!structTy.isInitialized()) {
32+
auto arrayTy = LLVM::LLVMArrayType::get(type, dimNum);
33+
auto res = structTy.setBody({arrayTy}, /*isPacked=*/false);
34+
assert(succeeded(res) &&
35+
"Unexpected failure from LLVMStructType::setBody.");
36+
}
37+
return structTy;
38+
}
39+
40+
// Get the LLVM type of a SYCL range or id type, given \p type - the type in
41+
// SYCL, \p name - the expected LLVM type name, \p converter - LLVM type
42+
// converter.
43+
template <typename T>
44+
static Type getSYCLRangeOrIDTy(T type, StringRef name,
45+
LLVMTypeConverter &converter) {
46+
unsigned dimNum = type.getDimension();
47+
auto structTy = LLVM::LLVMStructType::getIdentified(
48+
&converter.getContext(), name.str() + "." + std::to_string(dimNum));
49+
if (!structTy.isInitialized()) {
50+
auto res = structTy.setBody(getSYCLArrayTy(converter.getContext(), dimNum,
51+
converter.getIndexType()),
52+
/*isPacked=*/false);
53+
assert(succeeded(res) &&
54+
"Unexpected failure from LLVMStructType::setBody.");
55+
}
56+
return LLVM::LLVMPointerType::get(structTy);
57+
}
58+
} // namespace
59+
60+
void mlir::sycl::populateSYCLToLLVMConversionPatterns(
61+
LLVMTypeConverter &converter, RewritePatternSet &patterns) {
62+
converter.addConversion([&](mlir::sycl::IDType type) {
63+
return getSYCLRangeOrIDTy<mlir::sycl::IDType>(type, "class.cl::sycl::id",
64+
converter);
65+
});
66+
converter.addConversion([&](mlir::sycl::RangeType type) {
67+
return getSYCLRangeOrIDTy<mlir::sycl::RangeType>(
68+
type, "class.cl::sycl::range", converter);
69+
});
70+
}

polygeist/lib/polygeist/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ add_mlir_dialect_library(MLIRPolygeistTransforms
3636
MLIRPolygeist
3737
MLIRSideEffectInterfaces
3838
MLIRSCFToControlFlow
39+
MLIRSYCLDialect
3940
MLIRTransformUtils
4041
)

polygeist/lib/polygeist/Passes/ConvertPolygeistToLLVM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212
#include "PassDetails.h"
1313

14+
#include "SYCL/SYCLToLLVM.h"
1415
#include "mlir/Analysis/DataLayoutAnalysis.h"
1516
#include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h"
1617
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
@@ -630,6 +631,7 @@ struct ConvertPolygeistToLLVMPass
630631

631632
LLVMTypeConverter converter(&getContext(), options, &dataLayoutAnalysis);
632633
RewritePatternSet patterns(&getContext());
634+
sycl::populateSYCLToLLVMConversionPatterns(converter, patterns);
633635
populatePolygeistToLLVMConversionPatterns(converter, patterns);
634636
populateSCFToControlFlowConversionPatterns(patterns);
635637
cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) Intel
2+
3+
//===--- types.mlir ------------------------------------------------------===//
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
// RUN: cgeist %s -S -emit-llvm --args -fsycl-is-device | FileCheck %s
12+
13+
// CHECK-DAG: [[ARRAY_1:.*]] = type { [1 x i64] }
14+
// CHECK-DAG: [[ARRAY_2:.*]] = type { [2 x i64] }
15+
// CHECK-DAG: [[ID:.*]] = type { [[ARRAY_1]] }
16+
// CHECK-DAG: [[RANGE_1:.*]] = type { [[ARRAY_1]] }
17+
// CHECK-DAG: [[RANGE_2:.*]] = type { [[ARRAY_2]] }
18+
// CHECK: define void @test_id([[ID]]* %0, [[ID]]* %1)
19+
// CHECK: define void @test_range.1([[RANGE_1]]* %0)
20+
// CHECK: define void @test_range.2([[RANGE_2]]* %0)
21+
22+
module {
23+
func.func @test_id(%arg0: !sycl.id<1>, %arg1: !sycl.id<1>) {
24+
return
25+
}
26+
func.func @test_range.1(%arg0: !sycl.range<1>) {
27+
return
28+
}
29+
func.func @test_range.2(%arg0: !sycl.range<2>) {
30+
return
31+
}
32+
}

polygeist/tools/cgeist/Test/lit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
1717

1818
# suffixes: A list of file extensions to treat as test files. This is overriden
1919
# by individual lit.local.cfg files in the test subdirectories.
20-
config.suffixes = ['.c', '.cpp', '.cu']
20+
config.suffixes = ['.c', '.cpp', '.cu', '.mlir']
2121

2222
# excludes: A list of directories or files to exclude from the testsuite even
2323
# if they match the suffixes pattern.

0 commit comments

Comments
 (0)