Skip to content

Commit b5e85e6

Browse files
smilczekigcbot
authored andcommitted
PromoteBools::promoteFunction() setAttributes fix
promoteFunction method when setting attributes for the newly created function uses old function's attribute list, which in case some attributes got promoted causes the new function to inherit unpromoted attributes. This commit fixes it by defining a new method that iterates over the attribute list, checking for each whether it's been promoted and adding it to the new function.
1 parent 2e455a9 commit b5e85e6

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

IGC/AdaptorOCL/preprocess_spvir/PromoteBools.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ SPDX-License-Identifier: MIT
1212
#include "llvmWrapper/IR/DerivedTypes.h"
1313
#include "llvmWrapper/IR/Instructions.h"
1414
#include "llvmWrapper/IR/Type.h"
15+
#include "llvmWrapper/IR/Function.h"
16+
#include "llvmWrapper/IR/Attributes.h"
1517
#include "llvmWrapper/Support/Alignment.h"
1618
#include "llvmWrapper/Transforms/Utils/Cloning.h"
1719
#include <llvm/IR/Module.h>
@@ -500,6 +502,56 @@ Value* PromoteBools::getOrCreatePromotedValue(Value* value)
500502
return newValue;
501503
}
502504

505+
void PromoteBools::setPromotedAttributes(Function* newFunction, AttributeList& attributeList)
506+
{
507+
#if LLVM_VERSION_MAJOR >= 12
508+
auto getPromoted = [this, &newFunction](llvm::Attribute attr)
509+
{
510+
if (attr.isTypeAttribute())
511+
{
512+
return attr.getWithNewType(newFunction->getContext(),
513+
getOrCreatePromotedType(attr.getValueAsType()));
514+
}
515+
else
516+
{
517+
return attr;
518+
}
519+
};
520+
521+
// set function attributes
522+
AttrBuilder attrBuilder(newFunction->getContext());
523+
for (auto attr : IGCLLVM::getFnAttrs(attributeList))
524+
{
525+
attrBuilder.addAttribute(getPromoted(attr));
526+
}
527+
IGCLLVM::addFnAttrs(newFunction, attrBuilder);
528+
529+
// set return attributes
530+
attrBuilder.clear();
531+
for (auto attr : IGCLLVM::getRetAttrs(attributeList))
532+
{
533+
attrBuilder.addAttribute(getPromoted(attr));
534+
}
535+
IGCLLVM::addRetAttrs(newFunction, attrBuilder);
536+
537+
// set params' attributes
538+
for (size_t i = 0; i < newFunction->arg_size(); i++)
539+
{
540+
if (!attributeList.hasParamAttrs(i))
541+
{
542+
continue;
543+
}
544+
545+
attrBuilder.clear();
546+
for (auto attr : IGCLLVM::getParamAttrs(attributeList, i))
547+
{
548+
attrBuilder.addAttribute(getPromoted(attr));
549+
}
550+
newFunction->addParamAttrs(i, attrBuilder);
551+
}
552+
#endif // LLVM_VERSION_MAJOR >= 12
553+
}
554+
503555
Function* PromoteBools::promoteFunction(Function* function)
504556
{
505557
if (!function
@@ -516,8 +568,11 @@ Function* PromoteBools::promoteFunction(Function* function)
516568
function->getName() + ".promoted",
517569
function->getParent()
518570
);
571+
519572
newFunction->setCallingConv(function->getCallingConv());
520-
newFunction->setAttributes(function->getAttributes());
573+
574+
AttributeList attributeList = function->getAttributes();
575+
setPromotedAttributes(newFunction, attributeList);
521576

522577
// Clone and fix function body
523578
if (!function->isDeclaration())
@@ -532,9 +587,17 @@ Function* PromoteBools::promoteFunction(Function* function)
532587
newFunctionArgIt->setName(functionArgIt->getName());
533588
argsMap[&*functionArgIt++] = newFunctionArgIt++;
534589
}
590+
591+
// Create a copy of the promoted attributes list so that
592+
// we can reset it after CloneFunctionInto
593+
AttributeList newAttributeList = newFunction->getAttributes();
594+
535595
IGCLLVM::CloneFunctionInto(newFunction, function, argsMap,
536596
IGCLLVM::CloneFunctionChangeType::GlobalChanges, returns);
537597

598+
// CloneFunctionInto set the potentially unpromoted attrs again.
599+
newFunction->setAttributes(newAttributeList);
600+
538601
// Fix body
539602
for (auto& arg : function->args())
540603
{

IGC/AdaptorOCL/preprocess_spvir/PromoteBools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ namespace IGC
7979
});
8080
}
8181

82+
void setPromotedAttributes(llvm::Function* newFunction, llvm::AttributeList& attributeList);
83+
8284
llvm::Value* getOrCreatePromotedValue(llvm::Value* value);
8385
llvm::Function* promoteFunction(llvm::Function* function);
8486
llvm::GlobalVariable* promoteGlobalVariable(llvm::GlobalVariable* globalVariable);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2023 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; REQUIRES: system-linux
10+
; RUN: igc_opt -igc-promote-bools -S %s -o %t.ll
11+
; RUN: FileCheck %s --input-file=%t.ll
12+
13+
; CHECK-NOT: .unpromoted
14+
15+
%struct = type { [4 x <8 x i1*>], [4 x <8 x i1>*]* }
16+
17+
define spir_func void @prom_attr(%struct* byval(%struct) align 8 %0) {
18+
ret void
19+
}

IGC/WrapperLLVM/include/llvmWrapper/IR/Attributes.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ namespace IGCLLVM {
9292
#endif
9393
}
9494

95+
inline llvm::AttributeList addFnAttributes(llvm::AttributeList &Attrs, llvm::LLVMContext& C, llvm::AttrBuilder& B) {
96+
#if LLVM_VERSION_MAJOR >= 14
97+
return Attrs.addFnAttributes(C, B);
98+
#else
99+
return Attrs.addAttributes(C, llvm::AttributeList::FunctionIndex, B);
100+
#endif
101+
}
102+
103+
inline llvm::AttributeList addRetAttributes(llvm::AttributeList &Attrs, llvm::LLVMContext& C, llvm::AttrBuilder& B) {
104+
#if LLVM_VERSION_MAJOR >= 14
105+
return Attrs.addRetAttributes(C, B);
106+
#else
107+
return Attrs.addAttributes(C, llvm::AttributeList::ReturnIndex, B);
108+
#endif
109+
}
110+
95111
class AttrBuilder : public llvm::AttrBuilder {
96112
public:
97113
AttrBuilder() = delete;

IGC/WrapperLLVM/include/llvmWrapper/IR/Function.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ inline void addRetAttr(llvm::Function *F, llvm::Attribute::AttrKind Kind) {
3939
#endif
4040
}
4141

42+
inline void addRetAttrs(llvm::Function* F, llvm::AttrBuilder &B) {
43+
#if LLVM_VERSION_MAJOR < 14
44+
F->addAttributes(llvm::AttributeList::ReturnIndex, B);
45+
#else
46+
F->addRetAttrs(B);
47+
#endif
48+
}
49+
50+
inline void addFnAttrs(llvm::Function* F, llvm::AttrBuilder &B) {
51+
#if LLVM_VERSION_MAJOR < 14
52+
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
53+
#else
54+
F->addFnAttrs(B);
55+
#endif
56+
}
57+
4258
inline bool onlyWritesMemory(llvm::Function *F) {
4359
#if LLVM_VERSION_MAJOR < 14
4460
return F->doesNotReadMemory();

0 commit comments

Comments
 (0)