[llvm] Reland [AMDGPU] Add AMDGPU specific variadic operation MCExprs (PR #84562)

Janek van Oirschot via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 05:34:23 PDT 2024


================
@@ -0,0 +1,103 @@
+//===- AMDGPUMCExpr.cpp - AMDGPU specific MC expression classes -----------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "AMDGPUMCExpr.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCSymbol.h"
+#include "llvm/MC/MCValue.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/raw_ostream.h"
+#include <optional>
+
+using namespace llvm;
+
+const AMDGPUVariadicMCExpr *
+AMDGPUVariadicMCExpr::create(VariadicKind Kind, ArrayRef<const MCExpr *> Args,
+                             MCContext &Ctx) {
+  // Storage for the argument's 'const MCExpr*' allocated through MCContext new
+  // placement which means that AMDGPUVariadicMCExpr objects and all of its
+  // contents will now be allocated through MCContext new placement.
+  //
+  // Will result in an asan failure if allocated on the heap (e.g., through
+  // SmallVector's grow).
+  const MCExpr **CtxArgs = static_cast<const MCExpr **>(
+      Ctx.allocate(sizeof(const MCExpr *) * Args.size()));
+  std::uninitialized_copy(Args.begin(), Args.end(), CtxArgs);
----------------
JanekvO wrote:

I'm following convention for MCExpr to the extend that I can for AMDGPUVariadicMCExpr: no (target specific) MCExpr is provided the MCContext object and all explicitly have `create` functions to ensure the MCExpr object is allocated through MCContext (i.e., through the MCContext placement new). Additionally, [MCTargetExpr requires subclasses to have trivial destructors as it is never deallocated](https://github.com/llvm/llvm-project/blob/ffe41819e58365dfbe85a22556c0d9d284e746b9/llvm/include/llvm/MC/MCExpr.h#L654-L655). This is further reinforced as MCContext's deallocate function has an [empty body](https://github.com/llvm/llvm-project/blob/ffe41819e58365dfbe85a22556c0d9d284e746b9/llvm/include/llvm/MC/MCContext.h#L851).

Of course, I could be missing something but I do believe this is as good as it gets for supporting variadic arguments in a (target specific) MCExpr.

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


More information about the llvm-commits mailing list