[llvm] [RISCV] Lower llvm.clear_cache to __riscv_flush_icache for glibc targets (PR #93481)

Roger Ferrer Ibáñez via llvm-commits llvm-commits at lists.llvm.org
Thu May 30 06:38:14 PDT 2024


================
@@ -7117,7 +7122,48 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
     return lowerVPSpliceExperimental(Op, DAG);
   case ISD::EXPERIMENTAL_VP_REVERSE:
     return lowerVPReverseExperimental(Op, DAG);
+  case ISD::CLEAR_CACHE: {
+    assert(Subtarget.getTargetTriple().isOSGlibc() &&
+           "llvm.clear_cache only needs custom lower on glibc targets");
+    SDLoc DL(Op);
+    SDValue Flags = DAG.getConstant(0, DL, Subtarget.getXLenVT());
+    return emitFlushICache(DAG, Op.getOperand(0), Op.getOperand(1),
+                           Op.getOperand(2), Flags, DL);
   }
+  }
+}
+
+SDValue RISCVTargetLowering::emitFlushICache(SelectionDAG &DAG, SDValue InChain,
+                                             SDValue Start, SDValue End,
+                                             SDValue Flags, SDLoc DL) const {
+  TargetLowering::ArgListTy Args;
+  TargetLowering::ArgListEntry Entry;
+
+  // start
+  Entry.Node = Start;
+  Entry.Ty = PointerType::getUnqual(*DAG.getContext());
+  Args.push_back(Entry);
+
+  // end
+  Entry.Node = End;
+  Entry.Ty = PointerType::getUnqual(*DAG.getContext());
+  Args.push_back(Entry);
+
+  // flags
+  Entry.Node = Flags;
+  Entry.Ty = Type::getIntNTy(*DAG.getContext(), Subtarget.getXLen());
+  Args.push_back(Entry);
+
+  TargetLowering::CallLoweringInfo CLI(DAG);
+  EVT Ty = getPointerTy(DAG.getDataLayout());
+  CLI.setDebugLoc(DL).setChain(InChain).setLibCallee(
+      CallingConv::C, Type::getVoidTy(*DAG.getContext()),
+      DAG.getExternalSymbol("__riscv_flush_icache", Ty), std::move(Args));
----------------
rofirrim wrote:

Unfortunately the glibc RISC-V function has an extra parameter `flags` (https://www.gnu.org/software/libc/manual/html_node/RISC_002dV.html )

```cpp
void __riscv_flush_icache (void *start, void *end, unsigned long int flags);
```

while `__clear_cache` looks like this ( https://gcc.gnu.org/onlinedocs/gccint/Miscellaneous-routines.html#index-_005f_005fclear_005fcache )

```cpp
void __clear_cache (char *beg, char *end);
```

gcc is setting the `flags` to 0 when lowering `__builtin_clear_cache`.

https://github.com/llvm/llvm-project/pull/93481


More information about the llvm-commits mailing list