MLIR 24.0.0git
LowerFunctionDiscardablesToLLVM.cpp
Go to the documentation of this file.
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
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();
22
23 result.properties.sym_name = StringAttr::get(ctx, funcOp.getName());
24 result.properties.function_type = TypeAttr::get(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(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("llvm.") && odsAttrNames.contains(inherent))
44 inherentAttrs.set(inherent, attr.getValue()); // collect inherent attrs
45 else
46 result.discardableAttrs.push_back(attr);
47 }
48
49 // Convert collected inherent attrs into typed properties.
50 if (!inherentAttrs.empty()) {
51 DictionaryAttr dict = inherentAttrs.getDictionary(ctx);
52 auto emitError = [&] {
53 return funcOp.emitOpError("invalid llvm.func property");
54 };
55 if (failed(LLVM::LLVMFuncOp::setPropertiesFromAttr(result.properties, dict,
56 emitError))) {
57 return failure();
58 }
59 }
60 return result;
61}
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
DictionaryAttr getDictionary(MLIRContext *context) const
Return a dictionary attribute for the underlying dictionary.
Attribute set(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:179
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
FailureOr< LoweredLLVMFuncAttrs > lowerDiscardableAttrsForLLVMFunc(FunctionOpInterface funcOp, Type llvmFuncType)
Partition funcOp's discardables for llvm.func: sym_name, function_type, and typed properties from llv...
Result of lowering discardable attributes from a FunctionOpInterface to what llvm....