[clang] [flang] [llvm] [TargetVerifier][AMDGPU] Add TargetVerifier. (PR #123609)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 1 20:50:25 PDT 2025
================
@@ -0,0 +1,170 @@
+//===-- AMDGPUTargetVerifier.cpp - AMDGPU -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines target verifier interfaces that can be used for some
+// validation of input to the system, and for checking that transformations
+// haven't done something bad. In contrast to the Verifier or Lint, the
+// TargetVerifier looks for constructions invalid to a particular target
+// machine.
+//
+// To see what specifically is checked, look at an individual backend's
+// TargetVerifier.
+//
+//===----------------------------------------------------------------------===//
+
+#include "AMDGPU.h"
+
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/IntrinsicsAMDGPU.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Support/Debug.h"
+
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+// Check - We know that cond should be true, if not print an error message.
+#define Check(C, ...) \
+ do { \
+ if (!(C)) { \
+ TargetVerify::checkFailed(__VA_ARGS__); \
+ } \
+ } while (false)
+
+namespace llvm {
+
+class AMDGPUTargetVerify : public TargetVerify {
+public:
+ AMDGPUTargetVerify(Module *Mod) : TargetVerify(Mod) {}
+ bool run(Function &F) override;
+};
+
+static bool isShader(CallingConv::ID CC) {
+ switch (CC) {
+ case CallingConv::AMDGPU_VS:
+ case CallingConv::AMDGPU_LS:
+ case CallingConv::AMDGPU_HS:
+ case CallingConv::AMDGPU_ES:
+ case CallingConv::AMDGPU_GS:
+ case CallingConv::AMDGPU_PS:
+ case CallingConv::AMDGPU_CS_Chain:
+ case CallingConv::AMDGPU_CS_ChainPreserve:
+ case CallingConv::AMDGPU_CS:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool AMDGPUTargetVerify::run(Function &F) {
+ // Ensure shader calling convention returns void
+ if (isShader(F.getCallingConv()))
+ Check(F.getReturnType() == Type::getVoidTy(F.getContext()),
+ "Shaders must return void");
+
+ for (auto &BB : F) {
+
+ for (auto &I : BB) {
+
+ if (auto *CI = dyn_cast<CallInst>(&I)) {
+ // Ensure no kernel to kernel calls.
+ CallingConv::ID CalleeCC = CI->getCallingConv();
+ if (CalleeCC == CallingConv::AMDGPU_KERNEL) {
+ CallingConv::ID CallerCC =
+ CI->getParent()->getParent()->getCallingConv();
+ Check(CallerCC != CallingConv::AMDGPU_KERNEL,
----------------
shiltian wrote:
I think I already disallow this in the IR verifier.
https://github.com/llvm/llvm-project/pull/123609
More information about the llvm-commits
mailing list