mirror of https://github.com/llvm/circt.git
52 lines
1.9 KiB
C++
52 lines
1.9 KiB
C++
//===- CombToLLVM.cpp - Comb to LLVM Conversion Pass ----------------------===//
|
|
//
|
|
// 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 is the main Comb to LLVM Conversion Pass Implementation.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "circt/Conversion/CombToLLVM.h"
|
|
#include "circt/Dialect/Comb/CombOps.h"
|
|
#include "circt/Support/LLVM.h"
|
|
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
|
|
#include "mlir/Conversion/LLVMCommon/Pattern.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
using namespace mlir;
|
|
using namespace circt;
|
|
|
|
namespace {
|
|
/// Convert a comb::ParityOp to the LLVM dialect.
|
|
struct CombParityOpConversion : public ConvertToLLVMPattern {
|
|
explicit CombParityOpConversion(MLIRContext *ctx,
|
|
LLVMTypeConverter &typeConverter)
|
|
: ConvertToLLVMPattern(comb::ParityOp::getOperationName(), ctx,
|
|
typeConverter) {}
|
|
|
|
LogicalResult
|
|
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
auto parityOp = cast<comb::ParityOp>(op);
|
|
|
|
auto popCount =
|
|
LLVM::CtPopOp::create(rewriter, op->getLoc(), parityOp.getInput());
|
|
rewriter.replaceOpWithNewOp<LLVM::TruncOp>(
|
|
op, IntegerType::get(rewriter.getContext(), 1), popCount);
|
|
|
|
return success();
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
void circt::populateCombToLLVMConversionPatterns(LLVMTypeConverter &converter,
|
|
RewritePatternSet &patterns) {
|
|
patterns.add<CombParityOpConversion>(patterns.getContext(), converter);
|
|
}
|