Skip to content

Commit adf58bb

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 84d6176 commit adf58bb

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,9 @@ 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() ==
769+
CC_CHERILibCall &&
770+
FD->getDeclName().isIdentifier()) {
769771
assert(getASTContext().getTargetInfo().getTargetOpts().ABI != "cheriot-baremetal");
770772
return llvm::StringSwitch<bool>(FD->getName())
771773
.Case("memcpy", false)

clang/lib/AST/Mangle.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ 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() ==
126+
CC_CHERILibCall &&
127+
FD->getDeclName().isIdentifier()) {
126128
assert(ASTContext.getTargetInfo().getTargetOpts().ABI != "cheriot-baremetal");
127129
return llvm::StringSwitch<bool>(FD->getName())
128130
.Case("memcpy", false)

llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp

Lines changed: 11 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,13 @@ 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)
296+
return GA->getAliaseeObject();
297+
return GV;
298+
}
299+
292300
MachineBasicBlock *RISCVExpandPseudo::insertLoadOfImportTable(
293301
MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
294302
MCSymbol *ImportSymbol, MCSymbol *ExportSymbol, Register DestReg,
@@ -339,7 +347,7 @@ bool RISCVExpandPseudo::expandCompartmentCall(MachineBasicBlock &MBB,
339347
if (Callee.isGlobal()) {
340348
// If this is a global, check if it's in the same compartment. If so, we
341349
// want to lower as a direct ccall.
342-
auto *Fn = cast<Function>(Callee.getGlobal());
350+
auto *Fn = cast<Function>(resolveGlobalAlias(Callee.getGlobal()));
343351
if (MF->getFunction().hasFnAttribute("cheri-compartment") &&
344352
(Fn->getFnAttribute("cheri-compartment").getValueAsString() ==
345353
MF->getFunction()
@@ -391,7 +399,7 @@ bool RISCVExpandPseudo::expandCompartmentCall(MachineBasicBlock &MBB,
391399
computeAndAddLiveIns(LiveRegs, *NewMBB);
392400

393401
if (Callee.isGlobal()) {
394-
auto *Fn = cast<Function>(Callee.getGlobal());
402+
auto *Fn = dyn_cast<Function>(resolveGlobalAlias(Callee.getGlobal()));
395403
insertLoadOfImportTable(MBB, MBBI, Fn, RISCV::C6);
396404
} else {
397405
assert(Callee.isReg() && "Expected register operand");
@@ -416,7 +424,7 @@ bool RISCVExpandPseudo::expandLibraryCall(
416424
MachineInstr &MI = *MBBI;
417425
auto *MF = MBB.getParent();
418426
if (Callee.isGlobal()) {
419-
auto *Fn = cast<Function>(Callee.getGlobal());
427+
auto *Fn = cast<Function>(resolveGlobalAlias(Callee.getGlobal()));
420428
// If this is a global, check if it's defined in the same module and has a
421429
// compatible interrupt status. If so, we want to lower as a direct ccall.
422430
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)