1//===- LowerFunctionDiscardablesToLLVM.cpp - Func discardables to llvm ----===//
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 "mlir/Conversion/LLVMCommon/LowerFunctionDiscardablesToLLVM.h"
10#include "llvm/ADT/DenseSet.h"
11#include "llvm/Support/DebugLog.h"
12
13using namespace mlir;
14
15#define DEBUG_TYPE "lower-function-discardables-to-llvm"
16
17FailureOr<LoweredLLVMFuncAttrs>
18mlir::lowerDiscardableAttrsForLLVMFunc(FunctionOpInterface funcOp,
19 Type llvmFuncType) {
20 MLIRContext *ctx = funcOp->getContext();
21 LoweredLLVMFuncAttrs result;
22
23 result.properties.sym_name = StringAttr::get(context: ctx, bytes: funcOp.getName());
24 result.properties.function_type = TypeAttr::get(type: llvmFuncType);
25
26 llvm::SmallDenseSet<StringRef> odsAttrNames(
27 LLVM::LLVMFuncOp::getAttributeNames().begin(),
28 LLVM::LLVMFuncOp::getAttributeNames().end());
29
30 NamedAttrList inherentAttrs;
31
32 for (const NamedAttribute &attr :
33 funcOp->getDiscardableAttrDictionary().getValue()) {
34 StringRef attrName = attr.getName().strref();
35
36 if (odsAttrNames.contains(V: attrName)) {
37 LDBG() << "LLVM specific attributes: " << attrName
38 << "should use llvm.* prefix, discarding it";
39 continue;
40 }
41
42 StringRef inherent = attrName;
43 if (inherent.consume_front(Prefix: "llvm.") && odsAttrNames.contains(V: inherent))
44 inherentAttrs.set(name: inherent, value: attr.getValue()); // collect inherent attrs
45 else
46 result.discardableAttrs.push_back(newAttribute: attr);
47 }
48
49 // Convert collected inherent attrs into typed properties.
50 if (!inherentAttrs.empty()) {
51 DictionaryAttr dict = inherentAttrs.getDictionary(context: ctx);
52 auto emitError = [&] {
53 return funcOp.emitOpError(message: "invalid llvm.func property");
54 };
55 if (failed(Result: LLVM::LLVMFuncOp::setPropertiesFromAttr(prop&: result.properties, attr: dict,
56 emitError))) {
57 return failure();
58 }
59 }
60 return result;
61}
62