Skip to content

Commit 3a4dff1

Browse files
committed
[CHERIoT] Fix two issues that block compiling libc++'s extern string templates.
First, clang was crashing in during name mangling when attempting to mangling Decls with names that are not identifiers. This was only required for checking for specific fixed names, so we can just bypass it in that case. I have no idea how to test this, as my manual reductions don't contain an identifier-less Decl. Second, add global alias support to RISCVExpandPseudoInsts.
1 parent d350098 commit 3a4dff1

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ bool ItaniumMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
765765
.Default(true);
766766
}
767767

768-
if (FD->getType()->castAs<FunctionType>()->getCallConv() == CC_CHERILibCall) {
768+
if (FD->getType()->castAs<FunctionType>()->getCallConv() == CC_CHERILibCall &&
769+
FD->getDeclName().isIdentifier()) {
769770
assert(getASTContext().getTargetInfo().getTargetOpts().ABI != "cheriot-baremetal");
770771
return llvm::StringSwitch<bool>(FD->getName())
771772
.Case("memcpy", false)

clang/lib/AST/Mangle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
122122
// This is necessary because hasAttrs returns false for calling convention
123123
// attributes.
124124
if (auto *FD = dyn_cast<FunctionDecl>(D))
125-
if (FD->getType()->castAs<FunctionType>()->getCallConv() == CC_CHERILibCall) {
125+
if (FD->getType()->castAs<FunctionType>()->getCallConv() == CC_CHERILibCall &&
126+
FD->getDeclName().isIdentifier()) {
126127
assert(ASTContext.getTargetInfo().getTargetOpts().ABI != "cheriot-baremetal");
127128
return llvm::StringSwitch<bool>(FD->getName())
128129
.Case("memcpy", false)

llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/CodeGen/MachineInstrBuilder.h"
2525
#include "llvm/CodeGen/MachineOperand.h"
2626
#include "llvm/IR/CallingConv.h"
27+
#include "llvm/IR/GlobalAlias.h"
2728
#include "llvm/IR/GlobalVariable.h"
2829
#include "llvm/MC/MCContext.h"
2930
#include "llvm/Support/Compiler.h"
@@ -289,6 +290,12 @@ MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
289290
Fn->hasExternalLinkage(), CallImportTarget);
290291
}
291292

293+
static const GlobalValue *resolveGlobalAlias(const GlobalValue *GV) {
294+
auto *GA = dyn_cast<GlobalAlias>(GV);
295+
if (GA) return GA->getAliaseeObject();
296+
return GV;
297+
}
298+
292299
MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
293300
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
294301
MCSymbol *ImportSymbol, MCSymbol *ExportSymbol, Register DestReg,
@@ -339,7 +346,7 @@ bool RISCVExpandPseudo::expandCompartmentCall(MachineBasicBlock &MBB,
339346
if (Callee.isGlobal()) {
340347
// If this is a global, check if it's in the same compartment. If so, we
341348
// want to lower as a direct ccall.
342-
auto *Fn = cast<Function>(Callee.getGlobal());
349+
auto *Fn = cast<Function>(resolveGlobalAlias(Callee.getGlobal()));
343350
if (MF->getFunction().hasFnAttribute("cheri-compartment") &&
344351
(Fn->getFnAttribute("cheri-compartment").getValueAsString() ==
345352
MF->getFunction()
@@ -391,7 +398,7 @@ bool RISCVExpandPseudo::expandCompartmentCall(MachineBasicBlock &MBB,
391398
computeAndAddLiveIns(LiveRegs, *NewMBB);
392399

393400
if (Callee.isGlobal()) {
394-
auto *Fn = cast<Function>(Callee.getGlobal());
401+
auto *Fn = dyn_cast<Function>(resolveGlobalAlias(Callee.getGlobal()));
395402
insertLoadOfImportTable(MBB, MBBI, Fn, RISCV::C6);
396403
} else {
397404
assert(Callee.isReg() && "Expected register operand");
@@ -416,7 +423,7 @@ bool RISCVExpandPseudo::expandLibraryCall(
416423
MachineInstr &MI = *MBBI;
417424
auto *MF = MBB.getParent();
418425
if (Callee.isGlobal()) {
419-
auto *Fn = cast<Function>(Callee.getGlobal());
426+
auto *Fn = cast<Function>(resolveGlobalAlias(Callee.getGlobal()));
420427
// If this is a global, check if it's defined in the same module and has a
421428
// compatible interrupt status. If so, we want to lower as a direct ccall.
422429
if (!Fn->isDeclaration() && isSafeToDirectCall(MF->getFunction(), *Fn)) {

llvm/test/CodeGen/RISCV/cheri/cheri-mcu-call-libcall.ll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target datalayout = "e-m:e-pf200:64:64:64:32-p:32:32-i64:64-n32-S128-A200-P200-G
44
target triple = "riscv32-unknown-unknown"
55

66
; Function Attrs: minsize nounwind optsize
7+
; CHECK-LABEL: callFromNotLibcall:
78
define dso_local i32 @callFromNotLibcall() local_unnamed_addr addrspace(200) #0 {
89
entry:
910
; Check that these are direct calls via the import table, not going via the
@@ -22,12 +23,28 @@ entry:
2223
ret i32 %add
2324
}
2425

26+
; CHECK-LABEL: callViaAlias:
27+
define dso_local i32 @callViaAlias() local_unnamed_addr addrspace(200) #0 {
28+
entry:
29+
; CHECK: ccall bar_alias
30+
%call1 = tail call cherilibcallcc i32 @bar_alias() #2
31+
ret i32 %call1
32+
}
33+
34+
define cherilibcallcc i32 @bar() local_unnamed_addr addrspace(200) #0 {
35+
entry:
36+
ret i32 0
37+
}
38+
2539
; Function Attrs: minsize optsize
2640
declare cherilibcallcc i32 @add(i32, i32) local_unnamed_addr addrspace(200) #1
2741

2842
; Function Attrs: minsize optsize
2943
declare cherilibcallcc i32 @foo() local_unnamed_addr addrspace(200) #1
3044

45+
; CHECK: .set bar_alias, bar
46+
@bar_alias = weak_odr dso_local unnamed_addr alias void (), ptr addrspace(200) @bar
47+
3148
attributes #0 = { minsize nounwind optsize "cheri-compartment"="foo" "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="cheriot" "target-features"="+xcheri,-64bit,-relax,-save-restore,-no-rvc-hints" }
3249
attributes #1 = { minsize optsize "frame-pointer"="none" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="cheriot" "target-features"="+xcheri,-64bit,-relax,-save-restore,+no-rvc-hints" }
3350
attributes #2 = { minsize nounwind optsize }

0 commit comments

Comments
 (0)