57 lines · cpp
1//===- ControlFlowToSPIRVPass.cpp - ControlFlow to SPIR-V Pass ------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a pass to convert ControlFlow dialect to SPIR-V dialect.10//11//===----------------------------------------------------------------------===//12 13#include "mlir/Conversion/ControlFlowToSPIRV/ControlFlowToSPIRVPass.h"14 15#include "mlir/Conversion/ControlFlowToSPIRV/ControlFlowToSPIRV.h"16#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"17#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"18 19namespace mlir {20#define GEN_PASS_DEF_CONVERTCONTROLFLOWTOSPIRVPASS21#include "mlir/Conversion/Passes.h.inc"22} // namespace mlir23 24using namespace mlir;25 26namespace {27/// A pass converting MLIR ControlFlow operations into the SPIR-V dialect.28class ConvertControlFlowToSPIRVPass final29 : public impl::ConvertControlFlowToSPIRVPassBase<30 ConvertControlFlowToSPIRVPass> {31 using Base::Base;32 void runOnOperation() override;33};34} // namespace35 36void ConvertControlFlowToSPIRVPass::runOnOperation() {37 MLIRContext *context = &getContext();38 Operation *op = getOperation();39 40 auto targetAttr = spirv::lookupTargetEnvOrDefault(op);41 std::unique_ptr<ConversionTarget> target =42 SPIRVConversionTarget::get(targetAttr);43 44 SPIRVConversionOptions options;45 options.emulateLT32BitScalarTypes = this->emulateLT32BitScalarTypes;46 options.emulateUnsupportedFloatTypes = this->emulateUnsupportedFloatTypes;47 SPIRVTypeConverter typeConverter(targetAttr, options);48 49 // TODO: We should also take care of block argument type conversion.50 51 RewritePatternSet patterns(context);52 cf::populateControlFlowToSPIRVPatterns(typeConverter, patterns);53 54 if (failed(applyPartialConversion(op, *target, std::move(patterns))))55 return signalPassFailure();56}57