[llvm] [llubi] Implement experimental vector intrinsics (PR #206899)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 1 05:20:06 PDT 2026
================
@@ -810,6 +810,102 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
return AnyValue();
}
+ static BooleanKind getMaskLane(const AnyValue &Mask, size_t I) {
+ if (Mask.isAggregate())
+ return Mask.asAggregate()[I].asBoolean();
+ return Mask.asBoolean();
+ }
+
+ AnyValue callExperimentalVectorHistogramIntrinsic(CallBase &CB,
+ ArrayRef<AnyValue> Args,
+ Intrinsic::ID IID) {
+ struct Bucket {
+ MemoryObject *MO;
+ uint64_t Offset;
+ uint64_t Count;
+ };
+
+ const auto &Ptrs = Args[0].asAggregate();
+ const AnyValue &Update = Args[1];
+ const AnyValue &Mask = Args[2];
+ Type *ElemTy = CB.getArgOperand(1)->getType();
+ const uint64_t AccessSize = Ctx.getEffectiveTypeStoreSize(ElemTy);
+
+ SmallVector<Bucket, 8> Buckets;
+ for (size_t I = 0, E = Ptrs.size(); I != E; ++I) {
+ switch (getMaskLane(Mask, I)) {
+ case BooleanKind::False:
+ continue;
+ case BooleanKind::Poison:
+ reportImmediateUB()
+ << "Poison mask lane in experimental vector histogram intrinsic.";
+ return AnyValue();
+ case BooleanKind::True:
+ break;
+ }
+
+ if (Ptrs[I].isPoison()) {
+ reportImmediateUB() << "Poison pointer lane in experimental vector "
+ "histogram intrinsic.";
+ return AnyValue();
+ }
+
+ auto [MO, Offset] =
+ verifyMemAccess(Ptrs[I].asPointer(), AccessSize, Align(1),
+ /*IsStore=*/true);
+ if (!MO)
+ return AnyValue();
+
+ auto *It =
+ find_if(Buckets, [&, MO = MO, Offset = Offset](const Bucket &B) {
+ return B.MO == MO && B.Offset == Offset;
+ });
+ if (It == Buckets.end())
+ Buckets.push_back({MO, Offset, 1});
+ else
+ ++It->Count;
+ }
+
+ for (const auto &[MO, Offset, Count] : Buckets) {
+ AnyValue Old = Ctx.load(*MO, Offset, ElemTy);
+ AnyValue New;
+
+ if (Old.isPoison() || Update.isPoison()) {
+ New = AnyValue::poison();
+ } else {
+ const APInt &OldInt = Old.asInteger();
+ const APInt &UpdateInt = Update.asInteger();
+
+ switch (IID) {
+ case Intrinsic::experimental_vector_histogram_add:
+ New = OldInt + UpdateInt * APInt(UpdateInt.getBitWidth(), Count,
+ /*isSigned=*/false,
+ /*implicitTrunc=*/true);
+ break;
+ case Intrinsic::experimental_vector_histogram_uadd_sat: {
+ APInt Acc = OldInt;
+ for (uint64_t I = 0; I != Count; ++I)
+ Acc = Acc.uadd_sat(UpdateInt);
+ New = Acc;
+ break;
+ }
+ case Intrinsic::experimental_vector_histogram_umax:
+ New = APIntOps::umax(OldInt, UpdateInt);
+ break;
+ case Intrinsic::experimental_vector_histogram_umin:
+ New = APIntOps::umin(OldInt, UpdateInt);
+ break;
+ default:
+ llvm_unreachable("Unexpected histogram intrinsic ID");
+ }
+ }
+
+ Ctx.store(*MO, Offset, New, ElemTy);
----------------
dtcxzyw wrote:
The alignment is unspecified. What is the behaviour when the accessed ranges overlap?
Example:
```
call void @llvm.experimental.vector.histogram.add.v2p0.i32(<2 x ptr> <p, p + 2>)
```
`llvm.masked.scatter` says "Scatter with overlapping addresses is guaranteed to be ordered from least-significant to most-significant element". I think this also applies to the histogram intrinsic. Please add a test to demonstrate this behaviour.
https://github.com/llvm/llvm-project/pull/206899
More information about the llvm-commits
mailing list