//===- IndexToLLVM.cpp - Index to LLVM 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 // //===----------------------------------------------------------------------===// #include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h" #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" #include "mlir/Conversion/LLVMCommon/Pattern.h" #include "mlir/Dialect/Index/IR/IndexAttrs.h" #include "mlir/Dialect/Index/IR/IndexDialect.h" #include "mlir/Dialect/Index/IR/IndexOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Pass/Pass.h" usingnamespacemlir; usingnamespaceindex; namespace { //===----------------------------------------------------------------------===// // ConvertIndexCeilDivS //===----------------------------------------------------------------------===// /// Convert `ceildivs(n, m)` into `x = m > 0 ? -1 : 1` and then /// `n*m > 0 ? (n+x)/m + 1 : -(-n/m)`. struct ConvertIndexCeilDivS : mlir::ConvertOpToLLVMPattern<CeilDivSOp> { … }; //===----------------------------------------------------------------------===// // ConvertIndexCeilDivU //===----------------------------------------------------------------------===// /// Convert `ceildivu(n, m)` into `n == 0 ? 0 : (n-1)/m + 1`. struct ConvertIndexCeilDivU : mlir::ConvertOpToLLVMPattern<CeilDivUOp> { … }; //===----------------------------------------------------------------------===// // ConvertIndexFloorDivS //===----------------------------------------------------------------------===// /// Convert `floordivs(n, m)` into `x = m < 0 ? 1 : -1` and then /// `n*m < 0 ? -1 - (x-n)/m : n/m`. struct ConvertIndexFloorDivS : mlir::ConvertOpToLLVMPattern<FloorDivSOp> { … }; //===----------------------------------------------------------------------===// // CovnertIndexCast //===----------------------------------------------------------------------===// /// Convert a cast op. If the materialized index type is the same as the other /// type, fold away the op. Otherwise, truncate or extend the op as appropriate. /// Signed casts sign extend when the result bitwidth is larger. Unsigned casts /// zero extend when the result bitwidth is larger. template <typename CastOp, typename ExtOp> struct ConvertIndexCast : public mlir::ConvertOpToLLVMPattern<CastOp> { … }; ConvertIndexCastS; ConvertIndexCastU; //===----------------------------------------------------------------------===// // ConvertIndexCmp //===----------------------------------------------------------------------===// /// Assert that the LLVM comparison enum lines up with index's enum. static constexpr bool checkPredicates(LLVM::ICmpPredicate lhs, IndexCmpPredicate rhs) { … } static_assert …; struct ConvertIndexCmp : public mlir::ConvertOpToLLVMPattern<CmpOp> { … }; //===----------------------------------------------------------------------===// // ConvertIndexSizeOf //===----------------------------------------------------------------------===// /// Lower `index.sizeof` to a constant with the value of the index bitwidth. struct ConvertIndexSizeOf : public mlir::ConvertOpToLLVMPattern<SizeOfOp> { … }; //===----------------------------------------------------------------------===// // ConvertIndexConstant //===----------------------------------------------------------------------===// /// Convert an index constant. Truncate the value as appropriate. struct ConvertIndexConstant : public mlir::ConvertOpToLLVMPattern<ConstantOp> { … }; //===----------------------------------------------------------------------===// // Trivial Conversions //===----------------------------------------------------------------------===// ConvertIndexAdd; ConvertIndexSub; ConvertIndexMul; ConvertIndexDivS; ConvertIndexDivU; ConvertIndexRemS; ConvertIndexRemU; ConvertIndexMaxS; ConvertIndexMaxU; ConvertIndexMinS; ConvertIndexMinU; ConvertIndexShl; ConvertIndexShrS; ConvertIndexShrU; ConvertIndexAnd; ConvertIndexOr; ConvertIndexXor; ConvertIndexBoolConstant; } // namespace //===----------------------------------------------------------------------===// // Pattern Population //===----------------------------------------------------------------------===// void index::populateIndexToLLVMConversionPatterns( LLVMTypeConverter &typeConverter, RewritePatternSet &patterns) { … } //===----------------------------------------------------------------------===// // ODS-Generated Definitions //===----------------------------------------------------------------------===// namespace mlir { #define GEN_PASS_DEF_CONVERTINDEXTOLLVMPASS #include "mlir/Conversion/Passes.h.inc" } // namespace mlir //===----------------------------------------------------------------------===// // Pass Definition //===----------------------------------------------------------------------===// namespace { struct ConvertIndexToLLVMPass : public impl::ConvertIndexToLLVMPassBase<ConvertIndexToLLVMPass> { … }; } // namespace void ConvertIndexToLLVMPass::runOnOperation() { … } //===----------------------------------------------------------------------===// // ConvertToLLVMPatternInterface implementation //===----------------------------------------------------------------------===// namespace { /// Implement the interface to convert Index to LLVM. struct IndexToLLVMDialectInterface : public ConvertToLLVMPatternInterface { … }; } // namespace void mlir::index::registerConvertIndexToLLVMInterface( DialectRegistry ®istry) { … }