| 1 | //===- LowerToLLVMMetadata.cpp - CIR metadata lowering -----------===// |
| 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 | #include "LowerToLLVMMetadata.h" |
| 10 | #include "LowerToLLVMOpenCLMetadata.h" |
| 11 | |
| 12 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 13 | #include "mlir/IR/OperationSupport.h" |
| 14 | #include "clang/CIR/Dialect/IR/CIRAttrs.h" |
| 15 | #include "llvm/ADT/TypeSwitch.h" |
| 16 | |
| 17 | namespace cir { |
| 18 | namespace direct { |
| 19 | |
| 20 | FunctionMetadataLowering::FunctionMetadataLowering(mlir::MLIRContext *ctx) |
| 21 | : ctx(ctx) {} |
| 22 | |
| 23 | bool FunctionMetadataLowering::lower(mlir::NamedAttribute attr, |
| 24 | bool includeFunctionOnlyAttrs) { |
| 25 | return llvm::TypeSwitch<mlir::Attribute, bool>(attr.getValue()) |
| 26 | .Case<cir::OpenCLKernelArgMetadataAttr>( |
| 27 | caseFn: [&](cir::OpenCLKernelArgMetadataAttr clArgMetadata) { |
| 28 | if (includeFunctionOnlyAttrs) |
| 29 | lowerOpenCLKernelArgMetadata(clArgMetadata, entries&: functionMetadata); |
| 30 | return true; |
| 31 | }) |
| 32 | .Case<cir::MaxWorkGroupSizeAttr>( |
| 33 | caseFn: [&](cir::MaxWorkGroupSizeAttr maxWGSize) { |
| 34 | if (includeFunctionOnlyAttrs) |
| 35 | lower(maxWGSize); |
| 36 | return true; |
| 37 | }) |
| 38 | .Default(defaultResult: false); |
| 39 | } |
| 40 | |
| 41 | void FunctionMetadataLowering::appendAttrs( |
| 42 | llvm::SmallVectorImpl<mlir::NamedAttribute> &result) const { |
| 43 | if (!functionMetadata.empty()) { |
| 44 | result.push_back(Elt: mlir::NamedAttribute( |
| 45 | mlir::LLVM::LLVMFuncOp::getFunctionMetadataAttrName(name: mlir::OperationName( |
| 46 | mlir::LLVM::LLVMFuncOp::getOperationName(), ctx)), |
| 47 | mlir::ArrayAttr::get(context: ctx, value: functionMetadata))); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void FunctionMetadataLowering::lower(cir::MaxWorkGroupSizeAttr maxWGSize) { |
| 52 | LLVMMetadataNodeBuilder metadataBuilder(ctx); |
| 53 | unsigned sizes[] = {maxWGSize.getX(), maxWGSize.getY(), maxWGSize.getZ()}; |
| 54 | functionMetadata.push_back(Elt: mlir::LLVM::FunctionMetadataAttr::get( |
| 55 | context: ctx, metadataName: mlir::StringAttr::get(context: ctx, bytes: "max_work_group_size" ), |
| 56 | node: metadataBuilder.getI32Node(values: sizes))); |
| 57 | } |
| 58 | |
| 59 | } // namespace direct |
| 60 | } // namespace cir |
| 61 | |