CIRCT 22.0.0git
Loading...
Searching...
No Matches
CombToLLVM.cpp
Go to the documentation of this file.
1//===- CombToLLVM.cpp - Comb to LLVM Conversion Patterns ------------------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements Comb to LLVM conversion patterns.
10//
11//===----------------------------------------------------------------------===//
12
15#include "circt/Support/LLVM.h"
16#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
17#include "mlir/Conversion/LLVMCommon/Pattern.h"
18#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
19#include "mlir/Pass/Pass.h"
20#include "mlir/Transforms/DialectConversion.h"
21
22using namespace mlir;
23using namespace circt;
24
25namespace {
26
27//===----------------------------------------------------------------------===//
28// Comb Operation Conversion Patterns
29//===----------------------------------------------------------------------===//
30
31/// Convert a comb::ParityOp to the LLVM dialect.
32/// This is the only Comb operation that doesn't have a Comb-to-Arith pattern.
33struct CombParityOpConversion : public ConvertToLLVMPattern {
34 explicit CombParityOpConversion(MLIRContext *ctx,
35 LLVMTypeConverter &typeConverter)
36 : ConvertToLLVMPattern(comb::ParityOp::getOperationName(), ctx,
37 typeConverter) {}
38
39 LogicalResult
40 matchAndRewrite(Operation *op, ArrayRef<Value> operands,
41 ConversionPatternRewriter &rewriter) const override {
42 auto parityOp = cast<comb::ParityOp>(op);
43
44 auto popCount =
45 LLVM::CtPopOp::create(rewriter, op->getLoc(), parityOp.getInput());
46 rewriter.replaceOpWithNewOp<LLVM::TruncOp>(
47 op, IntegerType::get(rewriter.getContext(), 1), popCount);
48
49 return success();
50 }
51};
52
53} // namespace
54
55//===----------------------------------------------------------------------===//
56// Pattern Population Functions
57//===----------------------------------------------------------------------===//
58
59void circt::populateCombToLLVMConversionPatterns(LLVMTypeConverter &converter,
60 RewritePatternSet &patterns) {
61 // Only add patterns for operations that don't have Comb-to-Arith patterns
62 // Most Comb operations are handled by the Comb-to-Arith + Arith-to-LLVM
63 // pipeline
64 patterns.add<CombParityOpConversion>(patterns.getContext(), converter);
65}
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
void populateCombToLLVMConversionPatterns(mlir::LLVMTypeConverter &converter, RewritePatternSet &patterns)
Get the Comb to LLVM conversion patterns.
Definition comb.py:1