Skip to content
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

Reland: "[mlir][index][spirv] Add conversion for index to spirv" #69790

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions mlir/include/mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===- IndexToSPIRV.h - Index to SPIRV dialect conversion -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_CONVERSION_INDEXTOSPIRV_INDEXTOSPIRV_H
#define MLIR_CONVERSION_INDEXTOSPIRV_INDEXTOSPIRV_H

#include "mlir/Pass/Pass.h"
#include <memory>

namespace mlir {
class RewritePatternSet;
class SPIRVTypeConverter;
class Pass;

#define GEN_PASS_DECL_CONVERTINDEXTOSPIRVPASS
#include "mlir/Conversion/Passes.h.inc"

namespace index {
void populateIndexToSPIRVPatterns(SPIRVTypeConverter &converter,
RewritePatternSet &patterns);
std::unique_ptr<OperationPass<>> createConvertIndexToSPIRVPass();
} // namespace index
} // namespace mlir

#endif // MLIR_CONVERSION_INDEXTOSPIRV_INDEXTOSPIRV_H
1 change: 1 addition & 0 deletions mlir/include/mlir/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h"
#include "mlir/Conversion/GPUToVulkan/ConvertGPUToVulkanPass.h"
#include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
#include "mlir/Conversion/IndexToSPIRV/IndexToSPIRV.h"
#include "mlir/Conversion/LinalgToStandard/LinalgToStandard.h"
#include "mlir/Conversion/MathToFuncs/MathToFuncs.h"
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
Expand Down
22 changes: 22 additions & 0 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,28 @@ def ConvertIndexToLLVMPass : Pass<"convert-index-to-llvm"> {
];
}

//===----------------------------------------------------------------------===//
// ConvertIndexToSPIRVPass
//===----------------------------------------------------------------------===//

def ConvertIndexToSPIRVPass : Pass<"convert-index-to-spirv"> {
let summary = "Lower the `index` dialect to the `spirv` dialect.";
let description = [{
This pass lowers Index dialect operations to SPIR-V dialect operations.
Operation conversions are 1-to-1 except for the exotic divides: `ceildivs`,
`ceildivu`, and `floordivs`. The index bitwidth will be 32 or 64 as
specified by use-64bit-index.
}];

let dependentDialects = ["::mlir::spirv::SPIRVDialect"];

let options = [
Option<"use64bitIndex", "use-64bit-index",
"bool", /*default=*/"false",
"Use 64-bit integers to convert index types">
];
}

//===----------------------------------------------------------------------===//
// LinalgToStandard
//===----------------------------------------------------------------------===//
Expand Down
11 changes: 8 additions & 3 deletions mlir/include/mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ struct SPIRVConversionOptions {
/// values will be packed into one 32-bit value to be memory efficient.
bool emulateLT32BitScalarTypes{true};

/// Use 64-bit integers to convert index types.
bool use64bitIndex{false};

/// Whether to enable fast math mode during conversion. If true, various
/// patterns would assume no NaN/infinity numbers as inputs, and thus there
/// will be no special guards emitted to check and handle such cases.
bool enableFastMathMode{false};

/// Use 64-bit integers when converting index types.
bool use64bitIndex{false};
};

/// Type conversion from builtin types to SPIR-V types for shader interface.
Expand All @@ -77,6 +77,11 @@ class SPIRVTypeConverter : public TypeConverter {
/// Gets the SPIR-V correspondence for the standard index type.
Type getIndexType() const;

/// Gets the bitwidth of the index type when converted to SPIR-V.
unsigned getIndexTypeBitwidth() const {
return options.use64bitIndex ? 64 : 32;
}

const spirv::TargetEnv &getTargetEnv() const { return targetEnv; }

/// Returns the options controlling the SPIR-V type converter.
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_subdirectory(GPUToROCDL)
add_subdirectory(GPUToSPIRV)
add_subdirectory(GPUToVulkan)
add_subdirectory(IndexToLLVM)
add_subdirectory(IndexToSPIRV)
add_subdirectory(LinalgToStandard)
add_subdirectory(LLVMCommon)
add_subdirectory(MathToFuncs)
Expand Down
17 changes: 17 additions & 0 deletions mlir/lib/Conversion/IndexToSPIRV/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_mlir_conversion_library(MLIRIndexToSPIRV
IndexToSPIRV.cpp

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/IndexToSPIRV

DEPENDS
MLIRConversionPassIncGen

LINK_COMPONENTS
Core

LINK_LIBS PUBLIC
MLIRIndexDialect
MLIRSPIRVConversion
MLIRSPIRVDialect
)