[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
wangpc-pp wrote: > Can we implement this in `computeKnownBitsFromOperator/getRangeForIntrinsic`? > > https://github.com/llvm/llvm-project/blob/b21e3282864c9f7ad656c64bc375f5869ef76d19/llvm/lib/Analysis/ValueTracking.cpp#L1578-L1584 Thanks! We can implement this partly (will create another PR to do this) as we can't represent some assumptions related to runtime value `avl` like `vlhttps://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
dtcxzyw wrote: Can we implement this in `computeKnownBitsFromOperator/getRangeForIntrinsic`? https://github.com/llvm/llvm-project/blob/b21e3282864c9f7ad656c64bc375f5869ef76d19/llvm/lib/Analysis/ValueTracking.cpp#L1578-L1584 https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
wangpc-pp wrote: Ping for comments. https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
wangpc-pp wrote: > Also I guess most of RVV intrinsic could add `const` too, that could help > some generic optimization work better like CSE. This PR doesn't add the `const` attribute, I don't know if adding `const` may help to optimize. https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
kito-cheng wrote: Also I guess most of RVV intrinsic could add `const` too, that could help some generic optimization work better like CSE. https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
kito-cheng wrote: I guess we need add that at RVVEmitter::createbuilt...@riscvvemitter.cpp? [1] https://github.com/llvm/llvm-project/blob/main/clang/utils/TableGen/RISCVVEmitter.cpp#L418 [2] https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Basic/Builtins.h#L122-L124 [3] https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Basic/BuiltinsARM.def#L32 https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
wangpc-pp wrote: > I'm concerned that llvm.assume is handled differently than a branch to > unreachable in the middle end. Actually the CodeGen part is written by referring to the LLVM IR generated by unreachable way (https://godbolt.org/z/vf1v7f744). > Have you tested that these assumes have the intended effect? Yes, the end-to-end tests show that we can generate the same assemblies. I will add a test later. https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
topperc wrote: I'm concerned that llvm.assume is handled differently than a branch to unreachable in the middle end. Have you tested that these assumes have the intended effect? https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
wangpc-pp wrote: After some random thinking, I think these assumptions may need to be added to vsetvli/vsetvlimax LLVM intrinsics instead of adding them in Clang CodeGen. In this way, we can make other frontends like `flang/Rust/TVM/MLIR/...` benefit from these assumptions. Is it possible? I don't see a way to add similar attributes to intrinsics. https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
hiraditya wrote: cc: @alexey-bataev @nikolaypanchenko https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
kito-cheng wrote: Does it possible to add testcases to demonstrate that can improve optimization? https://github.com/llvm/llvm-project/pull/79975 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][RISCV] Add assumptions to vsetvli/vsetvlimax (PR #79975)
llvmbot wrote: @llvm/pr-subscribers-backend-risc-v @llvm/pr-subscribers-clang Author: Wang Pengcheng (wangpc-pp) Changes There are some assumptions of the return value of vsetvli/vsetvlimax, we add them via `llvm.assume` so that middle-end optimizations can benefit from them. --- Patch is 141.83 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/79975.diff 6 Files Affected: - (modified) clang/include/clang/Basic/riscv_vector.td (+97-3) - (modified) clang/test/CodeGen/RISCV/rvv-intrinsics-autogenerated/non-policy/non-overloaded/vsetvl.c (+575-44) - (modified) clang/test/CodeGen/RISCV/rvv-intrinsics-autogenerated/non-policy/non-overloaded/vsetvlmax.c (+268-44) - (modified) clang/test/CodeGen/RISCV/rvv-intrinsics-autogenerated/policy/non-overloaded/vsetvl.c (+575-44) - (modified) clang/test/CodeGen/RISCV/rvv-intrinsics-autogenerated/policy/non-overloaded/vsetvlmax.c (+268-44) - (modified) clang/test/CodeGen/RISCV/rvv-intrinsics-handcrafted/rvv-error.c (+8-2) ``diff diff --git a/clang/include/clang/Basic/riscv_vector.td b/clang/include/clang/Basic/riscv_vector.td index a00ca353588ed..84f61e3be4cfa 100644 --- a/clang/include/clang/Basic/riscv_vector.td +++ b/clang/include/clang/Basic/riscv_vector.td @@ -669,10 +669,104 @@ let HasBuiltinAlias = false, HasVL = false, HasMasked = false, MaskedPolicyScheme = NonePolicy, -Log2LMUL = [0], -ManualCodegen = [{IntrinsicTypes = {ResultType};}] in // Set XLEN type -{ +Log2LMUL = [0] in { + + let ManualCodegen = [{ +{ + // Set XLEN type + IntrinsicTypes = {ResultType}; + llvm::Function *F = CGM.getIntrinsic(ID, IntrinsicTypes); + llvm::Value *VSetVL = Builder.CreateCall(F, Ops, "vl"); + + const TargetInfo = getContext().getTargetInfo(); + auto VScale = TI.getVScaleRange(getContext().getLangOpts()); + + if (VScale && VScale->first && VScale->first == VScale->second) { +// Assumptions: +// Let: +// fixed_vl = __riscv_v_fixed_vlen / 8; +// We have: +// (avl > fixed_vl || vl == avl) +// and +// (avl < fixed_vl * 2 || vl == fixed_vl) +Value *FixedVL = llvm::ConstantInt::get(ResultType, VScale->first * 8); +Value *FixedVLx2 = +llvm::ConstantInt::get(ResultType, VScale->first * 8 * 2); +Value *Cond0 = Builder.CreateICmpULE( +Ops[0], ConstantInt::get(ResultType, VScale->first * 8)); + +BasicBlock *AssumptionBlock = +createBasicBlock("assumption", this->CurFn); +BasicBlock *AssumptionEndBlock = +createBasicBlock("assumption_end", this->CurFn); +Builder.CreateCondBr(Cond0, AssumptionBlock, AssumptionEndBlock); + +Builder.SetInsertPoint(AssumptionBlock); +Builder.CreateAssumption(Builder.CreateICmpEQ(VSetVL, Ops[0])); +Builder.CreateBr(AssumptionEndBlock); + +Builder.SetInsertPoint(AssumptionEndBlock); +Value *Assumption0 = Builder.CreateICmpULT(Ops[0], FixedVLx2); +Value *Assumption1 = Builder.CreateICmpEQ(VSetVL, FixedVL); +Builder.CreateAssumption(Builder.CreateSelect( +Assumption0, ConstantInt::getTrue(getLLVMContext()), Assumption1)); + } else { +// Assumptions: +// Let: +// min_vl = __riscv_v_min_vlen / 8 +// We have: +// (avl > min_vl || vl == avl) +Value *Cond0 = Builder.CreateICmpULE( +Ops[0], ConstantInt::get(ResultType, VScale->first * 8)); + +BasicBlock *AssumptionBlock = createBasicBlock("assumption", this->CurFn); +BasicBlock *AssumptionEndBlock = createBasicBlock("assumption_end", this->CurFn); +Builder.CreateCondBr(Cond0, AssumptionBlock, AssumptionEndBlock); + +Builder.SetInsertPoint(AssumptionBlock); +Value *Assumption = Builder.CreateICmpEQ(VSetVL, Ops[0]); +Builder.CreateAssumption(Assumption); +Builder.CreateBr(AssumptionEndBlock); + +Builder.SetInsertPoint(AssumptionEndBlock); + } + return VSetVL; +} +}] in def vsetvli : RVVBuiltin<"", "zzKzKz", "i">; + + let ManualCodegen = [{ +{ + // Set XLEN type + IntrinsicTypes = {ResultType}; + llvm::Function *F = CGM.getIntrinsic(ID, IntrinsicTypes); + llvm::Value *VSetVL = Builder.CreateCall(F, Ops, "vl"); + + const TargetInfo = getContext().getTargetInfo(); + auto VScale = TI.getVScaleRange(getContext().getLangOpts()); + + Value *Assumption; + if (VScale && VScale->first && VScale->first == VScale->second) +// Assumptions: +// Let: +// fixed_vl = __riscv_v_fixed_vlen / 8; +// We have: +// vlmax == fixed_vl +Assumption = Builder.CreateICmpEQ( +VSetVL, llvm::ConstantInt::get(ResultType, VScale->first * 8)); + else +// Assumptions: +// Let: