From nobody Sat Nov 23 18:18:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153703; cv=none; d=zohomail.com; s=zohoarc; b=aiZojJhd3pYzLOMjktnfxofgRhkhKbLw0nh/2IaF+nihJ34u36R0kkaNQZ1/3LddDTZCkhTR0Q+gDcNIPoDmkvdY3Oe4DQop1u/PoZ+RBVzR6ab7N+ClhHRAbUAmYiPdgJqxpanzyFOM/fUQJ96oh9w9rzRpfOPz4voFn/U/jTI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153703; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=OIlScO3BuH63uZxdmqbUwS+sjbYNsWyshmkeVqgGtWQ=; b=SfGwBv1NoYD1+5Gxex+2W4OXRL61SVrvK0LbjamEQ03cvR5f9DzO/zHE1tEZc6R8n5ueQfzwmrwxKJx0/XsbIXe6sktwKvr8NtGJRK0J61cwRX4zwzF4bQzZKamXMub5rKCeGroOx5KlxB/hj64VYNJVUK2GFMKa0ecZzyVFmfs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153703103340.9596052068604; Wed, 20 Nov 2024 17:48:23 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHs-0001cX-Tw; Wed, 20 Nov 2024 20:47:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHr-0001b4-8i for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHp-0004Xj-Ie for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=OIlScO3BuH63uZxdmqbUwS+sjbYNsWyshmkeVqgGtWQ=; b=mrO8wMR8oaQDdcZ JelEhQefGPshBw7N41+t9ly5Fx6J1+DhujzxT0HxjnTXZoVDY/DOO8wZK3lle/79GWxu43zp0qmFA 1y/hKzVMBJ/GmKZ78F4PWjgQUBsCQtTBKcCbPCZOL6IO6NaIg6F0K+GdZrUZ5UISl2tMeIuU6L458 xc=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 16/43] helper-to-tcg: PrepareForOptPass, Cull unused functions Date: Thu, 21 Nov 2024 02:49:20 +0100 Message-ID: <20241121014947.18666-17-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153704504116600 Content-Type: text/plain; charset="utf-8" Make an early pass over all functions in the input module and filter out functions with: 1. Invalid return type; 2. No helper-to-tcg annotation. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/PrepareForOptPass.h | 7 +- .../PrepareForOptPass/PrepareForOptPass.cpp | 93 +++++++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 7 +- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/subprojects/helper-to-tcg/include/PrepareForOptPass.h b/subpro= jects/helper-to-tcg/include/PrepareForOptPass.h index 5f9c059b97..8615625f09 100644 --- a/subprojects/helper-to-tcg/include/PrepareForOptPass.h +++ b/subprojects/helper-to-tcg/include/PrepareForOptPass.h @@ -29,9 +29,12 @@ =20 class PrepareForOptPass : public llvm::PassInfoMixin { AnnotationMapTy &ResultAnnotations; + bool TranslateAllHelpers; public: - PrepareForOptPass(AnnotationMapTy &ResultAnnotations) - : ResultAnnotations(ResultAnnotations) + PrepareForOptPass(AnnotationMapTy &ResultAnnotations, + bool TranslateAllHelpers) + : ResultAnnotations(ResultAnnotations), + TranslateAllHelpers(TranslateAllHelpers) { } llvm::PreservedAnalyses run(llvm::Module &M, diff --git a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp index 9f1d4df102..22509008c8 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp @@ -17,12 +17,17 @@ =20 #include #include +#include =20 #include #include #include +#include #include =20 +#include +#include + using namespace llvm; =20 static Expected parseAnnotationStr(StringRef Str, @@ -105,8 +110,96 @@ static void collectAnnotations(Module &M, AnnotationMa= pTy &ResultAnnotations) } } =20 +inline bool hasValidReturnTy(const Module &M, const Function *F) +{ + Type *RetTy =3D F->getReturnType(); + return RetTy =3D=3D Type::getVoidTy(F->getContext()) || + RetTy =3D=3D Type::getInt8Ty(M.getContext()) || + RetTy =3D=3D Type::getInt16Ty(M.getContext()) || + RetTy =3D=3D Type::getInt32Ty(M.getContext()) || + RetTy =3D=3D Type::getInt64Ty(M.getContext()); +} + +// Functions that should be removed: +// - No helper-to-tcg annotation (if TranslateAllHelpers =3D=3D false); +// - Invalid (non-integer/void) return type +static bool shouldRemoveFunction(const Module &M, const Function &F, + const AnnotationMapTy &AnnotationMap, + bool TranslateAllHelpers) +{ + if (F.isDeclaration()) { + return false; + } + + if (!hasValidReturnTy(M, &F)) { + return true; + } + + auto hasCorrectAnnotation =3D [](const Annotation &Ann) { + return Ann.Kind =3D=3D AnnotationKind::HelperToTcg; + }; + + std::queue Worklist; + std::set Visited; + Worklist.push(&F); + while (!Worklist.empty()) { + const Function *F =3D Worklist.front(); + Worklist.pop(); + if (F->isDeclaration() or Visited.find(F) !=3D Visited.end()) { + continue; + } + Visited.insert(F); + + // Check for llvm-to-tcg annotation + if (TranslateAllHelpers and F->getName().startswith("helper_")) { + return false; + } else { + auto It =3D AnnotationMap.find(F); + if (It !=3D AnnotationMap.end()) { + const auto &AnnotationVec =3D It->second; + auto Res =3D find_if(AnnotationVec, hasCorrectAnnotation); + if (Res !=3D AnnotationVec.end()) { + return false; + } + } + } + + // Push functions that call F to the worklist, this way we retain + // functions that are being called by functions with the llvm-to-t= cg + // annotation. + for (const User *U : F->users()) { + auto Call =3D dyn_cast(U); + if (!Call) { + continue; + } + const Function *ParentF =3D Call->getParent()->getParent(); + Worklist.push(ParentF); + } + } + + return true; +} + +static void cullUnusedFunctions(Module &M, AnnotationMapTy &Annotations, + bool TranslateAllHelpers) +{ + SmallVector FunctionsToRemove; + for (auto &F : M) { + if (shouldRemoveFunction(M, F, Annotations, TranslateAllHelpers)) { + FunctionsToRemove.push_back(&F); + } + } + + for (Function *F : FunctionsToRemove) { + Annotations.erase(F); + F->setComdat(nullptr); + F->deleteBody(); + } +} + PreservedAnalyses PrepareForOptPass::run(Module &M, ModuleAnalysisManager = &MAM) { collectAnnotations(M, ResultAnnotations); + cullUnusedFunctions(M, ResultAnnotations, TranslateAllHelpers); return PreservedAnalyses::none(); } diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index 3b9493bc73..dde3641ab3 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -47,6 +47,11 @@ cl::OptionCategory Cat("helper-to-tcg Options"); cl::opt InputFile(cl::Positional, cl::desc("[input LLVM modul= e]"), cl::cat(Cat)); =20 +// Options for PrepareForOptPass +cl::opt TranslateAllHelpers( + "translate-all-helpers", cl::init(false), + cl::desc("Translate all functions starting with helper_*"), cl::cat(Ca= t)); + // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such // as the width of the largest scalar/vector registers. Needed for consis= tent @@ -175,7 +180,7 @@ int main(int argc, char **argv) } =20 AnnotationMapTy Annotations; - MPM.addPass(PrepareForOptPass(Annotations)); + MPM.addPass(PrepareForOptPass(Annotations, TranslateAllHelpers)); =20 { FunctionPassManager FPM; --=20 2.45.2