Skip to content

Commit ac95b57

Browse files
committed
[IR] Add new function attribute nocreateundeforpoison
Also add a corresponding intrinsic property that can be used to mark intrinsics that do not introduce poison, for example simple arithmetic intrinsics that propagate poison just like a simple arithmetic instruction. As a smoke test this patch adds the new property to llvm.amdgcn.fmul.legacy.
1 parent f801b6f commit ac95b57

File tree

14 files changed

+55
-6
lines changed

14 files changed

+55
-6
lines changed

llvm/docs/LangRef.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,10 @@ For example:
27412741
``"nooutline"``
27422742
This attribute indicates that outlining passes should not modify the
27432743
function.
2744+
``nocreateundeforpoison``
2745+
This attribute indicates that the result of the function will not be undef
2746+
or poison if all arguments are not undef and not poison. Otherwise, it is
2747+
undefined behavior.
27442748

27452749
Call Site Attributes
27462750
----------------------

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ enum AttributeKindCodes {
801801
ATTR_KIND_CAPTURES = 102,
802802
ATTR_KIND_DEAD_ON_RETURN = 103,
803803
ATTR_KIND_SANITIZE_ALLOC_TOKEN = 104,
804+
ATTR_KIND_NO_CREATE_UNDEF_OR_POISON = 105,
804805
};
805806

806807
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ def NoCallback : EnumAttr<"nocallback", IntersectAnd, [FnAttr]>;
183183
/// Specify how the pointer may be captured.
184184
def Captures : IntAttr<"captures", IntersectCustom, [ParamAttr]>;
185185

186+
/// Result will not be undef or poison if all arguments are not undef and not
187+
/// poison.
188+
def NoCreateUndefOrPoison
189+
: EnumAttr<"nocreateundeforpoison", IntersectAnd, [FnAttr]>;
190+
186191
/// Function is not a source of divergence.
187192
def NoDivergenceSource : EnumAttr<"nodivergencesource", IntersectAnd, [FnAttr]>;
188193

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ def IntrSpeculatable : IntrinsicProperty;
186186
// defined by the hasSideEffects property of the TableGen Instruction class.
187187
def IntrHasSideEffects : IntrinsicProperty;
188188

189+
// Result will not be undef or poison if all arguments are not undef and not
190+
// poison.
191+
def IntrNoCreateUndefOrPoison : IntrinsicProperty;
192+
189193
//===----------------------------------------------------------------------===//
190194
// IIT constants and utils
191195
//===----------------------------------------------------------------------===//

llvm/include/llvm/IR/IntrinsicsAMDGPU.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def int_amdgcn_log_clamp : DefaultAttrsIntrinsic<
449449

450450
def int_amdgcn_fmul_legacy : ClangBuiltin<"__builtin_amdgcn_fmul_legacy">,
451451
DefaultAttrsIntrinsic<[llvm_float_ty], [llvm_float_ty, llvm_float_ty],
452-
[IntrNoMem, IntrSpeculatable, Commutative]
452+
[IntrNoMem, IntrSpeculatable, Commutative, IntrNoCreateUndefOrPoison]
453453
>;
454454

455455
// Fused single-precision multiply-add with legacy behaviour for the multiply,

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7446,8 +7446,10 @@ static bool canCreateUndefOrPoison(const Operator *Op, UndefPoisonKind Kind,
74467446
return false;
74477447
case Intrinsic::sshl_sat:
74487448
case Intrinsic::ushl_sat:
7449-
return includesPoison(Kind) &&
7450-
!shiftAmountKnownInRange(II->getArgOperand(1));
7449+
if (!includesPoison(Kind) ||
7450+
shiftAmountKnownInRange(II->getArgOperand(1)))
7451+
return false;
7452+
break;
74517453
case Intrinsic::fma:
74527454
case Intrinsic::fmuladd:
74537455
case Intrinsic::sqrt:
@@ -7496,7 +7498,8 @@ static bool canCreateUndefOrPoison(const Operator *Op, UndefPoisonKind Kind,
74967498
case Instruction::CallBr:
74977499
case Instruction::Invoke: {
74987500
const auto *CB = cast<CallBase>(Op);
7499-
return !CB->hasRetAttr(Attribute::NoUndef);
7501+
return !CB->hasRetAttr(Attribute::NoUndef) &&
7502+
!CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
75007503
}
75017504
case Instruction::InsertElement:
75027505
case Instruction::ExtractElement: {

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
22572257
return Attribute::Captures;
22582258
case bitc::ATTR_KIND_DEAD_ON_RETURN:
22592259
return Attribute::DeadOnReturn;
2260+
case bitc::ATTR_KIND_NO_CREATE_UNDEF_OR_POISON:
2261+
return Attribute::NoCreateUndefOrPoison;
22602262
}
22612263
}
22622264

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
956956
return bitc::ATTR_KIND_CAPTURES;
957957
case Attribute::DeadOnReturn:
958958
return bitc::ATTR_KIND_DEAD_ON_RETURN;
959+
case Attribute::NoCreateUndefOrPoison:
960+
return bitc::ATTR_KIND_NO_CREATE_UNDEF_OR_POISON;
959961
case Attribute::EndAttrKinds:
960962
llvm_unreachable("Can not encode end-attribute kinds marker.");
961963
case Attribute::None:

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ Function *CodeExtractor::constructFunctionDeclaration(
933933
case Attribute::CoroDestroyOnlyWhenComplete:
934934
case Attribute::CoroElideSafe:
935935
case Attribute::NoDivergenceSource:
936+
case Attribute::NoCreateUndefOrPoison:
936937
continue;
937938
// Those attributes should be safe to propagate to the extracted function.
938939
case Attribute::AlwaysInline:

llvm/test/Bitcode/attributes.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ define void @f_sanitize_alloc_token() sanitize_alloc_token {
521521
ret void;
522522
}
523523

524+
; CHECK: define void @f_no_create_undef_or_poison() #56
525+
define void @f_no_create_undef_or_poison() nocreateundeforpoison {
526+
ret void;
527+
}
528+
524529
; CHECK: define void @f87() [[FNRETTHUNKEXTERN:#[0-9]+]]
525530
define void @f87() fn_ret_thunk_extern { ret void }
526531

@@ -633,6 +638,7 @@ define void @dead_on_return(ptr dead_on_return %p) {
633638
; CHECK: attributes #53 = { sanitize_realtime }
634639
; CHECK: attributes #54 = { sanitize_realtime_blocking }
635640
; CHECK: attributes #55 = { sanitize_alloc_token }
641+
; CHECK: attributes #56 = { nocreateundeforpoison }
636642
; CHECK: attributes [[FNRETTHUNKEXTERN]] = { fn_ret_thunk_extern }
637643
; CHECK: attributes [[SKIPPROFILE]] = { skipprofile }
638644
; CHECK: attributes [[OPTDEBUG]] = { optdebug }

0 commit comments

Comments
 (0)