2 // Copyright 2016 Francisco Jerez
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
25 /// Utility functions for LLVM IR metadata introspection.
28 #ifndef CLOVER_LLVM_METADATA_HPP
29 #define CLOVER_LLVM_METADATA_HPP
31 #include "llvm/compat.hpp"
32 #include "util/algorithm.hpp"
35 #include <llvm/Config/llvm-config.h>
36 #include <llvm/IR/Module.h>
37 #include <llvm/IR/Metadata.h>
43 is_kernel(const ::llvm::Function &f) {
44 return f.getMetadata("kernel_arg_type");
47 inline iterator_range< ::llvm::MDNode::op_iterator>
48 get_kernel_metadata_operands(const ::llvm::Function &f,
49 const std::string &name) {
50 const auto data_node = f.getMetadata(name);
51 return range(data_node->op_begin(), data_node->op_end());
56 /// Extract the string metadata node \p name corresponding to the kernel
57 /// argument given by \p arg.
60 get_argument_metadata(const ::llvm::Function &f,
61 const ::llvm::Argument &arg,
62 const std::string &name) {
63 return ::llvm::cast< ::llvm::MDString>(
64 detail::get_kernel_metadata_operands(f, name)[arg.getArgNo()])
69 /// Return a vector with all CL kernel functions found in the LLVM
72 inline std::vector<const ::llvm::Function *>
73 get_kernels(const ::llvm::Module &mod) {
74 std::vector<const ::llvm::Function *> fs;
76 for (auto &f : mod.getFunctionList()) {
77 if (detail::is_kernel(f))