Skip to content

Commit 779ead3

Browse files
Merge branch 'extract_vec2' of https://github.com/abhishek-kaushik22/llvm-project into extract_vec2
2 parents 0f17a58 + fbc7ab3 commit 779ead3

16 files changed

+139
-305
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,9 @@ elementwise to the input.
647647

648648
Unless specified otherwise operation(±0) = ±0 and operation(±infinity) = ±infinity
649649

650+
The integer elementwise intrinsics, including ``__builtin_elementwise_popcount``,
651+
can be called in a ``constexpr`` context.
652+
650653
============================================== ====================================================================== =========================================
651654
Name Operation Supported element types
652655
============================================== ====================================================================== =========================================

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ Non-comprehensive list of changes in this release
375375
- ``__builtin_reduce_mul`` function can now be used in constant expressions.
376376
- ``__builtin_reduce_and`` function can now be used in constant expressions.
377377
- ``__builtin_reduce_or`` and ``__builtin_reduce_xor`` functions can now be used in constant expressions.
378+
- ``__builtin_elementwise_popcount`` function can now be used in constant expressions.
378379

379380
New Compiler Flags
380381
------------------

clang/include/clang/Basic/Builtins.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ def ElementwiseLog10 : Builtin {
13541354

13551355
def ElementwisePopcount : Builtin {
13561356
let Spellings = ["__builtin_elementwise_popcount"];
1357-
let Attributes = [NoThrow, Const, CustomTypeChecking];
1357+
let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
13581358
let Prototype = "void(...)";
13591359
}
13601360

clang/lib/AST/ExprConstant.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11005,6 +11005,7 @@ namespace {
1100511005
bool VisitUnaryImag(const UnaryOperator *E);
1100611006
bool VisitBinaryOperator(const BinaryOperator *E);
1100711007
bool VisitUnaryOperator(const UnaryOperator *E);
11008+
bool VisitCallExpr(const CallExpr *E);
1100811009
bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
1100911010
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
1101011011

@@ -11302,6 +11303,35 @@ static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
1130211303
return false;
1130311304
}
1130411305

11306+
bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
11307+
if (!IsConstantEvaluatedBuiltinCall(E))
11308+
return ExprEvaluatorBaseTy::VisitCallExpr(E);
11309+
11310+
switch (E->getBuiltinCallee()) {
11311+
default:
11312+
return false;
11313+
case Builtin::BI__builtin_elementwise_popcount: {
11314+
APValue Source;
11315+
if (!EvaluateAsRValue(Info, E->getArg(0), Source))
11316+
return false;
11317+
11318+
QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
11319+
unsigned SourceLen = Source.getVectorLength();
11320+
SmallVector<APValue, 4> ResultElements;
11321+
ResultElements.reserve(SourceLen);
11322+
11323+
for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
11324+
APSInt Elt = Source.getVectorElt(EltNum).getInt();
11325+
ResultElements.push_back(
11326+
APValue(APSInt(APInt(Info.Ctx.getIntWidth(DestEltTy), Elt.popcount()),
11327+
DestEltTy->isUnsignedIntegerOrEnumerationType())));
11328+
}
11329+
11330+
return Success(APValue(ResultElements.data(), ResultElements.size()), E);
11331+
}
11332+
}
11333+
}
11334+
1130511335
bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
1130611336
APValue Source;
1130711337
QualType SourceVecType = E->getSrcExpr()->getType();
@@ -13118,6 +13148,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1311813148
case Builtin::BI__builtin_popcountl:
1311913149
case Builtin::BI__builtin_popcountll:
1312013150
case Builtin::BI__builtin_popcountg:
13151+
case Builtin::BI__builtin_elementwise_popcount:
1312113152
case Builtin::BI__popcnt16: // Microsoft variants of popcount
1312213153
case Builtin::BI__popcnt:
1312313154
case Builtin::BI__popcnt64: {

clang/test/CodeGen/builtins-elementwise-math.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,9 @@ void test_builtin_elementwise_log2(float f1, float f2, double d1, double d2,
666666
vf2 = __builtin_elementwise_log2(vf1);
667667
}
668668

669-
void test_builtin_elementwise_popcount(si8 vi1, si8 vi2,
670-
long long int i1, long long int i2, short si,
671-
_BitInt(31) bi1, _BitInt(31) bi2) {
672-
673-
669+
void test_builtin_elementwise_popcount(si8 vi1, si8 vi2, long long int i1,
670+
long long int i2, short si,
671+
_BitInt(31) bi1, _BitInt(31) bi2) {
674672
// CHECK: [[I1:%.+]] = load i64, ptr %i1.addr, align 8
675673
// CHECK-NEXT: call i64 @llvm.ctpop.i64(i64 [[I1]])
676674
i2 = __builtin_elementwise_popcount(i1);
@@ -693,7 +691,7 @@ void test_builtin_elementwise_popcount(si8 vi1, si8 vi2,
693691
// CHECK-NEXT: call i32 @llvm.ctpop.i32(i32 [[IA1]])
694692
b = __builtin_elementwise_popcount(int_as_one);
695693

696-
// CHECK: call i32 @llvm.ctpop.i32(i32 -10)
694+
// CHECK: store i32 30, ptr @b, align 4
697695
b = __builtin_elementwise_popcount(-10);
698696

699697
// CHECK: [[SI:%.+]] = load i16, ptr %si.addr, align 2

clang/test/Sema/constant_builtins_vector.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,23 @@ static_assert(__builtin_reduce_xor((vector4int){(int)0x11111111, (int)0x22222222
797797
static_assert(__builtin_reduce_xor((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0xFFFFFFFFFFFFFFFFL);
798798
static_assert(__builtin_reduce_xor((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU);
799799
static_assert(__builtin_reduce_xor((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFUL);
800+
801+
static_assert(__builtin_bit_cast(unsigned, __builtin_elementwise_popcount((vector4char){1, 2, 3, 4})) == (LITTLE_END ? 0x01020101 : 0x01010201));
802+
static_assert(__builtin_bit_cast(unsigned long long, __builtin_elementwise_popcount((vector4short){0, 0x0F0F, ~0, ~0x0F0F})) == (LITTLE_END ? 0x0008001000080000 : 0x0000000800100008));
803+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){1, 2, 3, 4})) == 5);
804+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){0, 0xF0F0, ~0, ~0xF0F0})) == 16 * sizeof(int));
805+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4long){1L, 2L, 3L, 4L})) == 5L);
806+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4long){0L, 0xF0F0L, ~0L, ~0xF0F0L})) == 16 * sizeof(long long));
807+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4uint){1U, 2U, 3U, 4U})) == 5U);
808+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4uint){0U, 0xF0F0U, ~0U, ~0xF0F0U})) == 16 * sizeof(int));
809+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4ulong){1UL, 2UL, 3UL, 4UL})) == 5UL);
810+
static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4ulong){0ULL, 0xF0F0ULL, ~0ULL, ~0xF0F0ULL})) == 16 * sizeof(unsigned long long));
811+
static_assert(__builtin_elementwise_popcount(0) == 0);
812+
static_assert(__builtin_elementwise_popcount(0xF0F0) == 8);
813+
static_assert(__builtin_elementwise_popcount(~0) == 8 * sizeof(int));
814+
static_assert(__builtin_elementwise_popcount(0U) == 0);
815+
static_assert(__builtin_elementwise_popcount(0xF0F0U) == 8);
816+
static_assert(__builtin_elementwise_popcount(~0U) == 8 * sizeof(int));
817+
static_assert(__builtin_elementwise_popcount(0L) == 0);
818+
static_assert(__builtin_elementwise_popcount(0xF0F0L) == 8);
819+
static_assert(__builtin_elementwise_popcount(~0LL) == 8 * sizeof(long long));

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,6 @@ static inline unsigned getVLOpNum(const MCInstrDesc &Desc) {
208208
return Desc.getNumOperands() - Offset;
209209
}
210210

211-
static inline unsigned getTailExpandUseRegNo(const FeatureBitset &FeatureBits) {
212-
// For Zicfilp, PseudoTAIL should be expanded to a software guarded branch.
213-
// It means to use t2(x7) as rs1 of JALR to expand PseudoTAIL.
214-
return FeatureBits[RISCV::FeatureStdExtZicfilp] ? RISCV::X7 : RISCV::X6;
215-
}
216-
217211
static inline unsigned getSEWOpNum(const MCInstrDesc &Desc) {
218212
const uint64_t TSFlags = Desc.TSFlags;
219213
assert(hasSEWOp(TSFlags));

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI,
124124
MCRegister Ra;
125125
if (MI.getOpcode() == RISCV::PseudoTAIL) {
126126
Func = MI.getOperand(0);
127-
Ra = RISCVII::getTailExpandUseRegNo(STI.getFeatureBits());
127+
Ra = RISCV::X6;
128+
// For Zicfilp, PseudoTAIL should be expanded to a software guarded branch.
129+
// It means to use t2(x7) as rs1 of JALR to expand PseudoTAIL.
130+
if (STI.hasFeature(RISCV::FeatureStdExtZicfilp))
131+
Ra = RISCV::X7;
128132
} else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
129133
Func = MI.getOperand(1);
130134
Ra = MI.getOperand(0).getReg();

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 33 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "RISCVInstrInfo.h"
14-
#include "MCTargetDesc/RISCVBaseInfo.h"
1514
#include "MCTargetDesc/RISCVMatInt.h"
1615
#include "RISCV.h"
1716
#include "RISCVMachineFunctionInfo.h"
@@ -2928,7 +2927,6 @@ bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
29282927

29292928
// Enum values indicating how an outlined call should be constructed.
29302929
enum MachineOutlinerConstructionID {
2931-
MachineOutlinerTailCall,
29322930
MachineOutlinerDefault
29332931
};
29342932

@@ -2937,118 +2935,46 @@ bool RISCVInstrInfo::shouldOutlineFromFunctionByDefault(
29372935
return MF.getFunction().hasMinSize();
29382936
}
29392937

2940-
static bool isCandidatePatchable(const MachineBasicBlock &MBB) {
2941-
const MachineFunction *MF = MBB.getParent();
2942-
const Function &F = MF->getFunction();
2943-
return F.getFnAttribute("fentry-call").getValueAsBool() ||
2944-
F.hasFnAttribute("patchable-function-entry");
2945-
}
2946-
2947-
static bool isMIReadsReg(const MachineInstr &MI, const TargetRegisterInfo *TRI,
2948-
unsigned RegNo) {
2949-
return MI.readsRegister(RegNo, TRI) ||
2950-
MI.getDesc().hasImplicitUseOfPhysReg(RegNo);
2951-
}
2952-
2953-
static bool isMIModifiesReg(const MachineInstr &MI,
2954-
const TargetRegisterInfo *TRI, unsigned RegNo) {
2955-
return MI.modifiesRegister(RegNo, TRI) ||
2956-
MI.getDesc().hasImplicitDefOfPhysReg(RegNo);
2957-
}
2958-
2959-
static bool cannotInsertTailCall(const MachineBasicBlock &MBB) {
2960-
if (!MBB.back().isReturn())
2961-
return true;
2962-
if (isCandidatePatchable(MBB))
2963-
return true;
2964-
2965-
// If the candidate reads the pre-set register
2966-
// that can be used for expanding PseudoTAIL instruction,
2967-
// then we cannot insert tail call.
2968-
const TargetSubtargetInfo &STI = MBB.getParent()->getSubtarget();
2969-
unsigned TailExpandUseRegNo =
2970-
RISCVII::getTailExpandUseRegNo(STI.getFeatureBits());
2971-
for (const MachineInstr &MI : MBB) {
2972-
if (isMIReadsReg(MI, STI.getRegisterInfo(), TailExpandUseRegNo))
2973-
return true;
2974-
if (isMIModifiesReg(MI, STI.getRegisterInfo(), TailExpandUseRegNo))
2975-
break;
2976-
}
2977-
return false;
2978-
}
2979-
2980-
static std::optional<MachineOutlinerConstructionID>
2981-
analyzeCandidate(outliner::Candidate &C) {
2982-
// If last instruction is return then we can rely on
2983-
// the verification already performed in the getOutliningTypeImpl.
2984-
if (C.back().isReturn()) {
2985-
assert(!cannotInsertTailCall(*C.getMBB()) &&
2986-
"The candidate who uses return instruction must be outlined "
2987-
"using tail call");
2988-
return MachineOutlinerTailCall;
2989-
}
2990-
2991-
auto CandidateUsesX5 = [](outliner::Candidate &C) {
2992-
const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
2993-
if (std::any_of(C.begin(), C.end(), [TRI](const MachineInstr &MI) {
2994-
return isMIModifiesReg(MI, TRI, RISCV::X5);
2995-
}))
2996-
return true;
2997-
return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
2998-
};
2999-
3000-
if (!CandidateUsesX5(C))
3001-
return MachineOutlinerDefault;
3002-
3003-
return std::nullopt;
3004-
}
3005-
30062938
std::optional<std::unique_ptr<outliner::OutlinedFunction>>
30072939
RISCVInstrInfo::getOutliningCandidateInfo(
30082940
const MachineModuleInfo &MMI,
30092941
std::vector<outliner::Candidate> &RepeatedSequenceLocs,
30102942
unsigned MinRepeats) const {
30112943

3012-
// Each RepeatedSequenceLoc is identical.
3013-
outliner::Candidate &Candidate = RepeatedSequenceLocs[0];
3014-
auto CandidateInfo = analyzeCandidate(Candidate);
3015-
if (!CandidateInfo)
3016-
RepeatedSequenceLocs.clear();
2944+
// First we need to filter out candidates where the X5 register (IE t0) can't
2945+
// be used to setup the function call.
2946+
auto CannotInsertCall = [](outliner::Candidate &C) {
2947+
const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
2948+
return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
2949+
};
2950+
2951+
llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall);
30172952

30182953
// If the sequence doesn't have enough candidates left, then we're done.
30192954
if (RepeatedSequenceLocs.size() < MinRepeats)
30202955
return std::nullopt;
30212956

3022-
unsigned InstrSizeCExt =
3023-
Candidate.getMF()->getSubtarget<RISCVSubtarget>().hasStdExtCOrZca() ? 2
3024-
: 4;
3025-
unsigned CallOverhead = 0, FrameOverhead = 0;
3026-
3027-
MachineOutlinerConstructionID MOCI = CandidateInfo.value();
3028-
switch (MOCI) {
3029-
case MachineOutlinerDefault:
3030-
// call t0, function = 8 bytes.
3031-
CallOverhead = 8;
3032-
// jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
3033-
FrameOverhead = InstrSizeCExt;
3034-
break;
3035-
case MachineOutlinerTailCall:
3036-
// tail call = auipc + jalr in the worst case without linker relaxation.
3037-
CallOverhead = 4 + InstrSizeCExt;
3038-
// Using tail call we move ret instruction from caller to callee.
3039-
FrameOverhead = 0;
3040-
break;
3041-
}
2957+
unsigned SequenceSize = 0;
2958+
2959+
for (auto &MI : RepeatedSequenceLocs[0])
2960+
SequenceSize += getInstSizeInBytes(MI);
30422961

2962+
// call t0, function = 8 bytes.
2963+
unsigned CallOverhead = 8;
30432964
for (auto &C : RepeatedSequenceLocs)
3044-
C.setCallInfo(MOCI, CallOverhead);
2965+
C.setCallInfo(MachineOutlinerDefault, CallOverhead);
30452966

3046-
unsigned SequenceSize = 0;
3047-
for (auto &MI : Candidate)
3048-
SequenceSize += getInstSizeInBytes(MI);
2967+
// jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
2968+
unsigned FrameOverhead = 4;
2969+
if (RepeatedSequenceLocs[0]
2970+
.getMF()
2971+
->getSubtarget<RISCVSubtarget>()
2972+
.hasStdExtCOrZca())
2973+
FrameOverhead = 2;
30492974

30502975
return std::make_unique<outliner::OutlinedFunction>(
3051-
RepeatedSequenceLocs, SequenceSize, FrameOverhead, MOCI);
2976+
RepeatedSequenceLocs, SequenceSize, FrameOverhead,
2977+
MachineOutlinerDefault);
30522978
}
30532979

30542980
outliner::InstrType
@@ -3069,8 +2995,15 @@ RISCVInstrInfo::getOutliningTypeImpl(const MachineModuleInfo &MMI,
30692995
return F.needsUnwindTableEntry() ? outliner::InstrType::Illegal
30702996
: outliner::InstrType::Invisible;
30712997

3072-
if (cannotInsertTailCall(*MBB) &&
3073-
(MI.isReturn() || isMIModifiesReg(MI, TRI, RISCV::X5)))
2998+
// We need support for tail calls to outlined functions before return
2999+
// statements can be allowed.
3000+
if (MI.isReturn())
3001+
return outliner::InstrType::Illegal;
3002+
3003+
// Don't allow modifying the X5 register which we use for return addresses for
3004+
// these outlined functions.
3005+
if (MI.modifiesRegister(RISCV::X5, TRI) ||
3006+
MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5))
30743007
return outliner::InstrType::Illegal;
30753008

30763009
// Make sure the operands don't reference something unsafe.
@@ -3106,9 +3039,6 @@ void RISCVInstrInfo::buildOutlinedFrame(
31063039
}
31073040
}
31083041

3109-
if (OF.FrameConstructionID == MachineOutlinerTailCall)
3110-
return;
3111-
31123042
MBB.addLiveIn(RISCV::X5);
31133043

31143044
// Add in a return instruction to the end of the outlined frame.
@@ -3122,13 +3052,6 @@ MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall(
31223052
Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
31233053
MachineFunction &MF, outliner::Candidate &C) const {
31243054

3125-
if (C.CallConstructionID == MachineOutlinerTailCall) {
3126-
It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(RISCV::PseudoTAIL))
3127-
.addGlobalAddress(M.getNamedValue(MF.getName()),
3128-
/*Offset=*/0, RISCVII::MO_CALL));
3129-
return It;
3130-
}
3131-
31323055
// Add in a call instruction to the outlined function at the given location.
31333056
It = MBB.insert(It,
31343057
BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)

0 commit comments

Comments
 (0)