MLIR 23.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 : funcOp->getDiscardableAttrs()) {
33 StringRef attrName = attr.getName().strref();
34
35 if (odsAttrNames.contains(attrName)) {
36 LDBG() << "LLVM specific attributes: " << attrName
37 << "should use llvm.* prefix, discarding it";
38 continue;
39 }
40
41 StringRef inherent = attrName;
42 if (inherent.consume_front("llvm.") && odsAttrNames.contains(inherent))
43 inherentAttrs.set(inherent, attr.getValue()); // collect inherent attrs
44 else
45 result.discardableAttrs.push_back(attr);
46 }
47
48 // Convert collected inherent attrs into typed properties.
49 if (!inherentAttrs.empty()) {
50 DictionaryAttr dict = inherentAttrs.getDictionary(ctx);
51 auto emitError = [&] {
52 return funcOp.emitOpError("invalid llvm.func property");
53 };
54 if (failed(LLVM::LLVMFuncOp::setPropertiesFromAttr(result.properties, dict,
55 emitError))) {
56 return failure();
57 }
58 }
59 return result;
60}
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
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....