| 1 | //===- LowerToLLVMOpenCLMetadata.cpp - OpenCL 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 "LowerToLLVMOpenCLMetadata.h" |
| 10 | #include "LowerToLLVMMetadata.h" |
| 11 | |
| 12 | #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 14 | #include "mlir/IR/Builders.h" |
| 15 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | |
| 18 | namespace cir { |
| 19 | namespace direct { |
| 20 | |
| 21 | namespace { |
| 22 | using KernelArgStringMetadataGetter = |
| 23 | mlir::ArrayAttr (cir::OpenCLKernelArgMetadataAttr::*)() const; |
| 24 | |
| 25 | struct KernelArgStringMetadataMapping { |
| 26 | llvm::StringLiteral metadataName; |
| 27 | KernelArgStringMetadataGetter getMetadata; |
| 28 | bool optional; |
| 29 | }; |
| 30 | |
| 31 | static unsigned getOpenCLArgInfoAddressSpace(cir::LangAddressSpace as) { |
| 32 | switch (as) { |
| 33 | case cir::LangAddressSpace::Default: |
| 34 | case cir::LangAddressSpace::OffloadPrivate: |
| 35 | return 0; |
| 36 | case cir::LangAddressSpace::OffloadGlobal: |
| 37 | return 1; |
| 38 | case cir::LangAddressSpace::OffloadConstant: |
| 39 | return 2; |
| 40 | case cir::LangAddressSpace::OffloadLocal: |
| 41 | return 3; |
| 42 | case cir::LangAddressSpace::OffloadGeneric: |
| 43 | return 4; |
| 44 | case cir::LangAddressSpace::OffloadGlobalDevice: |
| 45 | return 5; |
| 46 | case cir::LangAddressSpace::OffloadGlobalHost: |
| 47 | return 6; |
| 48 | } |
| 49 | llvm_unreachable("unknown CIR language address space" ); |
| 50 | } |
| 51 | |
| 52 | static mlir::LLVM::MDNodeAttr |
| 53 | getAddrSpaceMetadataNode(cir::OpenCLKernelArgMetadataAttr clArgMetadata, |
| 54 | const LLVMMetadataNodeBuilder &metadataBuilder) { |
| 55 | llvm::SmallVector<unsigned> addrSpaces; |
| 56 | for (cir::LangAddressSpaceAttr addressSpace : |
| 57 | clArgMetadata.getAddrSpace().getAsRange<cir::LangAddressSpaceAttr>()) |
| 58 | addrSpaces.push_back(Elt: getOpenCLArgInfoAddressSpace(as: addressSpace.getValue())); |
| 59 | return metadataBuilder.getI32Node(values: addrSpaces); |
| 60 | } |
| 61 | |
| 62 | static void addOpenCLKernelArgFunctionMetadata( |
| 63 | mlir::MLIRContext *ctx, llvm::SmallVectorImpl<mlir::Attribute> &entries, |
| 64 | llvm::StringRef name, mlir::LLVM::MDNodeAttr node) { |
| 65 | entries.push_back(Elt: mlir::LLVM::FunctionMetadataAttr::get( |
| 66 | context: ctx, metadataName: mlir::StringAttr::get(context: ctx, bytes: name), node)); |
| 67 | } |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | void lowerOpenCLKernelArgMetadata( |
| 72 | cir::OpenCLKernelArgMetadataAttr clArgMetadata, |
| 73 | llvm::SmallVectorImpl<mlir::Attribute> &entries) { |
| 74 | mlir::MLIRContext *ctx = clArgMetadata.getContext(); |
| 75 | LLVMMetadataNodeBuilder metadataBuilder(ctx); |
| 76 | |
| 77 | addOpenCLKernelArgFunctionMetadata( |
| 78 | ctx, entries, name: "kernel_arg_addr_space" , |
| 79 | node: getAddrSpaceMetadataNode(clArgMetadata, metadataBuilder)); |
| 80 | |
| 81 | static constexpr KernelArgStringMetadataMapping stringMetadataMappings[] = { |
| 82 | {.metadataName: "kernel_arg_access_qual" , |
| 83 | .getMetadata: &cir::OpenCLKernelArgMetadataAttr::getAccessQual, |
| 84 | /*optional=*/false}, |
| 85 | {.metadataName: "kernel_arg_type" , .getMetadata: &cir::OpenCLKernelArgMetadataAttr::getType, |
| 86 | /*optional=*/false}, |
| 87 | {.metadataName: "kernel_arg_base_type" , .getMetadata: &cir::OpenCLKernelArgMetadataAttr::getBaseType, |
| 88 | /*optional=*/false}, |
| 89 | {.metadataName: "kernel_arg_type_qual" , .getMetadata: &cir::OpenCLKernelArgMetadataAttr::getTypeQual, |
| 90 | /*optional=*/false}, |
| 91 | {.metadataName: "kernel_arg_name" , .getMetadata: &cir::OpenCLKernelArgMetadataAttr::getName, |
| 92 | /*optional=*/true}, |
| 93 | }; |
| 94 | |
| 95 | for (const KernelArgStringMetadataMapping &mapping : stringMetadataMappings) { |
| 96 | mlir::ArrayAttr metadata = (clArgMetadata.*mapping.getMetadata)(); |
| 97 | if (mapping.optional && !metadata) |
| 98 | continue; |
| 99 | addOpenCLKernelArgFunctionMetadata(ctx, entries, name: mapping.metadataName, |
| 100 | node: metadataBuilder.getStringNode(attrs: metadata)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static void createOpenCLVersionNamedMetadata(mlir::ModuleOp module, |
| 105 | llvm::StringRef name, |
| 106 | mlir::LLVM::MDNodeAttr node) { |
| 107 | mlir::MLIRContext *ctx = module.getContext(); |
| 108 | mlir::OpBuilder builder(ctx); |
| 109 | builder.setInsertionPointToEnd(module.getBody()); |
| 110 | mlir::LLVM::NamedMetadataOp::create(builder, location: module.getLoc(), metadata_name: name, |
| 111 | nodes: mlir::ArrayAttr::get(context: ctx, value: node)); |
| 112 | } |
| 113 | |
| 114 | static void emitOpenCLVersionMetadata(mlir::ModuleOp module, |
| 115 | llvm::StringRef metadataName, |
| 116 | mlir::Attribute versionAttr) { |
| 117 | auto version = mlir::cast<cir::OpenCLVersionAttr>(Val&: versionAttr); |
| 118 | mlir::MLIRContext *ctx = module.getContext(); |
| 119 | LLVMMetadataNodeBuilder metadataBuilder(ctx); |
| 120 | |
| 121 | unsigned versionMetadata[] = { |
| 122 | static_cast<unsigned>(version.getMajorVersion()), |
| 123 | static_cast<unsigned>(version.getMinorVersion())}; |
| 124 | createOpenCLVersionNamedMetadata(module, name: metadataName, |
| 125 | node: metadataBuilder.getI32Node(values: versionMetadata)); |
| 126 | } |
| 127 | |
| 128 | void lowerOpenCLModuleMetadataAttrs(mlir::ModuleOp module) { |
| 129 | struct OpenCLModuleMetadataMapping { |
| 130 | llvm::StringRef cirAttrName; |
| 131 | llvm::StringRef llvmMetadataName; |
| 132 | }; |
| 133 | |
| 134 | const OpenCLModuleMetadataMapping moduleMetadataMappings[] = { |
| 135 | {.cirAttrName: cir::CIRDialect::getOpenCLVersionAttrName(), .llvmMetadataName: "opencl.ocl.version" }, |
| 136 | {.cirAttrName: cir::CIRDialect::getOpenCLCXXVersionAttrName(), .llvmMetadataName: "opencl.cxx.version" }, |
| 137 | {.cirAttrName: cir::CIRDialect::getOpenCLSPIRVersionAttrName(), .llvmMetadataName: "opencl.spir.version" }, |
| 138 | }; |
| 139 | |
| 140 | for (const OpenCLModuleMetadataMapping &mapping : moduleMetadataMappings) { |
| 141 | mlir::Attribute version = module->getAttr(name: mapping.cirAttrName); |
| 142 | if (!version) |
| 143 | continue; |
| 144 | emitOpenCLVersionMetadata(module, metadataName: mapping.llvmMetadataName, versionAttr: version); |
| 145 | module->removeAttr(name: mapping.cirAttrName); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } // namespace direct |
| 150 | } // namespace cir |
| 151 | |