This commit introduces a new MathToEmitC conversion pass that lowers selected math operations from the Math dialect to the emitc.call_opaque operation in the EmitC dialect. **Supported Math Operations:** The following operations are converted: - math.floor -> emitc.call_opaque<"floor"> - math.round -> emitc.call_opaque<"round"> - math.exp -> emitc.call_opaque<"exp"> - math.cos -> emitc.call_opaque<"cos"> - math.sin -> emitc.call_opaque<"sin"> - math.acos -> emitc.call_opaque<"acos"> - math.asin -> emitc.call_opaque<"asin"> - math.atan2 -> emitc.call_opaque<"atan2"> - math.ceil -> emitc.call_opaque<"ceil"> - math.absf -> emitc.call_opaque<"fabs"> - math.powf -> emitc.call_opaque<"pow"> **Target Language Standards:** The pass supports targeting different language standards: - C99: Generates calls with suffixes (e.g., floorf, fabsf) for single-precision floats. - CPP11: Prepends std:: to functions (e.g., std::floor, std::fabs). **Design Decisions:** The pass uses emitc.call_opaque instead of emitc.call to better emulate C-style function overloading. emitc.call_opaque does not require a unique type signature, making it more suitable for operations like <math.h> functions that may be overloaded for different types. This design choice ensures compatibility with C/C++ conventions.
54 lines
1.8 KiB
C++
54 lines
1.8 KiB
C++
//===- MathToEmitCPass.cpp - Math to EmitC Pass -----------------*- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements a pass to convert the Math dialect to the EmitC dialect.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Conversion/MathToEmitC/MathToEmitCPass.h"
|
|
#include "mlir/Conversion/MathToEmitC/MathToEmitC.h"
|
|
#include "mlir/Dialect/EmitC/IR/EmitC.h"
|
|
#include "mlir/Dialect/Math/IR/Math.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
namespace mlir {
|
|
#define GEN_PASS_DEF_CONVERTMATHTOEMITC
|
|
#include "mlir/Conversion/Passes.h.inc"
|
|
} // namespace mlir
|
|
|
|
using namespace mlir;
|
|
namespace {
|
|
|
|
// Replaces Math operations with `emitc.call_opaque` operations.
|
|
struct ConvertMathToEmitC
|
|
: public impl::ConvertMathToEmitCBase<ConvertMathToEmitC> {
|
|
using ConvertMathToEmitCBase::ConvertMathToEmitCBase;
|
|
|
|
public:
|
|
void runOnOperation() final;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
void ConvertMathToEmitC::runOnOperation() {
|
|
ConversionTarget target(getContext());
|
|
target.addLegalOp<emitc::CallOpaqueOp>();
|
|
|
|
target.addIllegalOp<math::FloorOp, math::ExpOp, math::RoundOp, math::CosOp,
|
|
math::SinOp, math::Atan2Op, math::CeilOp, math::AcosOp,
|
|
math::AsinOp, math::AbsFOp, math::PowFOp>();
|
|
|
|
RewritePatternSet patterns(&getContext());
|
|
populateConvertMathToEmitCPatterns(patterns, languageTarget);
|
|
|
|
if (failed(
|
|
applyPartialConversion(getOperation(), target, std::move(patterns))))
|
|
signalPassFailure();
|
|
}
|