|
| 1 | +//===--------------- CallExprRewriterCUBLAS.h -----------------------------===// |
| 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 | +#ifndef DPCT_REWRITERS_CALL_EXPR_REWRITER_CUBLAS_H |
| 10 | +#define DPCT_REWRITERS_CALL_EXPR_REWRITER_CUBLAS_H |
| 11 | + |
| 12 | +#include "RuleInfra/CallExprRewriter.h" |
| 13 | +#include "RuleInfra/CallExprRewriterCommon.h" |
| 14 | +#include <string> |
| 15 | + |
| 16 | +namespace clang { |
| 17 | +namespace dpct { |
| 18 | + |
| 19 | +template <class ArgT> class BufferOrUSMPtrCallArgPrinter { |
| 20 | + ArgT Arg; |
| 21 | + std::string DataType; |
| 22 | + |
| 23 | +public: |
| 24 | + BufferOrUSMPtrCallArgPrinter(ArgT &&Arg, std::string DataType) |
| 25 | + : Arg(std::forward<ArgT>(Arg)), DataType(DataType) {} |
| 26 | + template <class StreamT> void print(StreamT &Stream) const { |
| 27 | + if (DpctGlobalInfo::getUsmLevel() == UsmLevel::UL_None) { |
| 28 | + Stream << MapNames::getLibraryHelperNamespace() |
| 29 | + << "rvalue_ref_to_lvalue_ref(" << MapNames::getDpctNamespace() |
| 30 | + << "get_buffer<" << DataType << ">("; |
| 31 | + clang::dpct::print(Stream, Arg); |
| 32 | + Stream << "))"; |
| 33 | + } else { |
| 34 | + if (DataType == "std::complex<float>" || |
| 35 | + DataType == "std::complex<double>") |
| 36 | + Stream << "(" << DataType << "*)"; |
| 37 | + if constexpr (std::is_same_v<ArgT, const Expr *>) |
| 38 | + clang::dpct::print(Stream, Arg->IgnoreCasts()); |
| 39 | + else |
| 40 | + clang::dpct::print(Stream, Arg); |
| 41 | + } |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +template <class ArgT> |
| 46 | +std::function<BufferOrUSMPtrCallArgPrinter<ArgT>(const CallExpr *)> |
| 47 | +makeBufferOrUSMPtrCallArgCreator(std::function<ArgT(const CallExpr *)> Arg, |
| 48 | + std::string DataType) { |
| 49 | + return PrinterCreator<BufferOrUSMPtrCallArgPrinter<ArgT>, |
| 50 | + std::function<ArgT(const CallExpr *)>, |
| 51 | + std::function<std::string(const CallExpr *)>>( |
| 52 | + Arg, [=](const CallExpr *) { return DataType; }); |
| 53 | +} |
| 54 | + |
| 55 | +class ScalarInputValuePrinter { |
| 56 | + const Expr *Arg; |
| 57 | + const Expr *Handle; |
| 58 | + std::string DataType; |
| 59 | + |
| 60 | +public: |
| 61 | + ScalarInputValuePrinter(const Expr *&&Arg, const Expr *&&Handle, |
| 62 | + std::string DataType) |
| 63 | + : Arg(std::forward<const Expr *>(Arg)), |
| 64 | + Handle(std::forward<const Expr *>(Handle)), DataType(DataType) {} |
| 65 | + template <class StreamT> void print(StreamT &Stream) const { |
| 66 | + const auto *UO = dyn_cast_or_null<UnaryOperator>(Arg->IgnoreImpCasts()); |
| 67 | + const auto *COCE = dyn_cast<CXXOperatorCallExpr>(Arg->IgnoreImpCasts()); |
| 68 | + if ((UO && UO->getOpcode() == UO_AddrOf && UO->getSubExpr()) || |
| 69 | + (COCE && COCE->getOperator() == OO_Amp && COCE->getArg(0))) { |
| 70 | + const Expr *Sub = UO ? UO->getSubExpr() : COCE->getArg(0); |
| 71 | + if (DataType == "std::complex<float>" || |
| 72 | + DataType == "std::complex<double>") { |
| 73 | + Stream << DataType << "("; |
| 74 | + clang::dpct::print(Stream, Sub); |
| 75 | + Stream << ".x(), "; |
| 76 | + clang::dpct::print(Stream, Sub); |
| 77 | + Stream << ".y())"; |
| 78 | + } else { |
| 79 | + clang::dpct::print(Stream, Sub); |
| 80 | + } |
| 81 | + } else { |
| 82 | + Stream << MapNames::getLibraryHelperNamespace() << "get_value("; |
| 83 | + clang::dpct::print(Stream, Arg); |
| 84 | + Stream << ", "; |
| 85 | + if (needExtraParensInMemberExpr(Handle)) { |
| 86 | + Stream << "("; |
| 87 | + clang::dpct::print(Stream, Handle); |
| 88 | + Stream << ")->get_queue())"; |
| 89 | + } else { |
| 90 | + clang::dpct::print(Stream, Handle); |
| 91 | + Stream << "->get_queue())"; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +std::function<ScalarInputValuePrinter( |
| 98 | + const CallExpr |
| 99 | + *)> inline makeScalarInputValueCreator(std:: |
| 100 | + function<const Expr *( |
| 101 | + const CallExpr *)> |
| 102 | + Arg, |
| 103 | + std::function<const Expr *( |
| 104 | + const CallExpr *)> |
| 105 | + Handle, |
| 106 | + std::string DataType) { |
| 107 | + return PrinterCreator<ScalarInputValuePrinter, |
| 108 | + std::function<const Expr *(const CallExpr *)>, |
| 109 | + std::function<const Expr *(const CallExpr *)>, |
| 110 | + std::function<std::string(const CallExpr *)>>( |
| 111 | + Arg, Handle, [=](const CallExpr *) { return DataType; }); |
| 112 | +} |
| 113 | + |
| 114 | +#define BUFFER_OR_USM_PTR(Arg, T) makeBufferOrUSMPtrCallArgCreator(Arg, T) |
| 115 | +#define SCALAR_INPUT(Arg, T) makeScalarInputValueCreator(Arg, ARG(0), T) |
| 116 | + |
| 117 | +typedef std::unordered_map<std::string, |
| 118 | + std::shared_ptr<CallExprRewriterFactoryBase>> |
| 119 | + RewriterMap; |
| 120 | + |
| 121 | +RewriterMap createCUBLASLevel1RewriterMap(); |
| 122 | +RewriterMap createCUBLASLevel2RewriterMap(); |
| 123 | +RewriterMap createCUBLASLevel3RewriterMap(); |
| 124 | +RewriterMap createCUBLASHelperRewriterMap(); |
| 125 | +RewriterMap createCUBLASExtRewriterMap(); |
| 126 | +RewriterMap createCUBLASLtRewriterMap(); |
| 127 | + |
| 128 | +} // namespace dpct |
| 129 | +} // namespace clang |
| 130 | + |
| 131 | +#endif // DPCT_REWRITERS_CALL_EXPR_REWRITER_CUBLAS_H |
0 commit comments