Blob Blame History Raw
From 68ed5f52759df4d7677edb2c80e6c799377c43f6 Mon Sep 17 00:00:00 2001
From: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Date: Sat, 15 Apr 2023 03:41:21 -0400
Subject: [PATCH 6/8] Handle argmemonly attribute change in LLVM16

Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
---
 compiler/compiler_test.go    | 22 +++++++++++++++-------
 compiler/testdata/channel.ll |  6 +++---
 compiler/testdata/go1.21.ll  | 16 ++++++++--------
 compiler/testdata/zeromap.ll |  6 +++---
 4 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/compiler/compiler_test.go b/compiler/compiler_test.go
index 92ce31b0..d6147b78 100644
--- a/compiler/compiler_test.go
+++ b/compiler/compiler_test.go
@@ -4,11 +4,11 @@ import (
 	"flag"
 	"go/types"
 	"os"
-	"strconv"
 	"strings"
 	"testing"
 
 	"github.com/tinygo-org/tinygo/compileopts"
+	"github.com/tinygo-org/tinygo/compiler/llvmutil"
 	"github.com/tinygo-org/tinygo/goenv"
 	"github.com/tinygo-org/tinygo/loader"
 	"tinygo.org/x/go-llvm"
@@ -154,12 +154,7 @@ func fuzzyEqualIR(s1, s2 string) bool {
 // stripped out.
 func filterIrrelevantIRLines(lines []string) []string {
 	var out []string
-	llvmVersion, err := strconv.Atoi(strings.Split(llvm.Version, ".")[0])
-	if err != nil {
-		// Note: this should never happen and if it does, it will always happen
-		// for a particular build because llvm.Version is a constant.
-		panic(err)
-	}
+	llvmVersion := llvmutil.Major()
 	for _, line := range lines {
 		line = strings.Split(line, ";")[0]    // strip out comments/info
 		line = strings.TrimRight(line, "\r ") // drop '\r' on Windows and remove trailing spaces from comments
@@ -174,6 +169,19 @@ func filterIrrelevantIRLines(lines []string) []string {
 			// Right now test outputs are for LLVM 14 and higher.
 			continue
 		}
+		if llvmVersion < 16 {
+			if strings.HasSuffix(line, "memory(argmem: readwrite) }") {
+				line = strings.Replace(line, "{ ", "{ argmemonly ", 1)
+				line = strings.Replace(line, "memory(argmem: readwrite) ", "", 1)
+			}
+			if strings.HasSuffix(line, "memory(none)") {
+				line = strings.Replace(
+					line,
+					"speculatable willreturn memory(none)",
+					"readnone speculatable willreturn",
+					1)
+			}
+		}
 		out = append(out, line)
 	}
 	return out
diff --git a/compiler/testdata/channel.ll b/compiler/testdata/channel.ll
index 9749effc..8ff64bd4 100644
--- a/compiler/testdata/channel.ll
+++ b/compiler/testdata/channel.ll
@@ -31,12 +31,12 @@ entry:
   ret void
 }
 
-; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
 declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #3
 
 declare void @runtime.chanSend(ptr dereferenceable_or_null(32), ptr, ptr dereferenceable_or_null(24), ptr) #1
 
-; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
 declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #3
 
 ; Function Attrs: nounwind
@@ -113,5 +113,5 @@ declare { i32, i1 } @runtime.tryChanSelect(ptr, ptr, i32, i32, ptr) #1
 attributes #0 = { allockind("alloc,zeroed") allocsize(0) "alloc-family"="runtime.alloc" "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
 attributes #1 = { "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
 attributes #2 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
-attributes #3 = { argmemonly nocallback nofree nosync nounwind willreturn }
+attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
 attributes #4 = { nounwind }
diff --git a/compiler/testdata/go1.21.ll b/compiler/testdata/go1.21.ll
index d65c75f4..c677298f 100644
--- a/compiler/testdata/go1.21.ll
+++ b/compiler/testdata/go1.21.ll
@@ -138,7 +138,7 @@ entry:
   ret void
 }
 
-; Function Attrs: argmemonly nocallback nofree nounwind willreturn writeonly
+; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
 declare void @llvm.memset.p0.i32(ptr nocapture writeonly, i8, i32, i1 immarg) #3
 
 ; Function Attrs: nounwind
@@ -156,24 +156,24 @@ entry:
 
 declare void @runtime.hashmapClear(ptr dereferenceable_or_null(40), ptr) #1
 
-; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
 declare i32 @llvm.smin.i32(i32, i32) #4
 
-; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
 declare i8 @llvm.umin.i8(i8, i8) #4
 
-; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
 declare i32 @llvm.umin.i32(i32, i32) #4
 
-; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
 declare i32 @llvm.smax.i32(i32, i32) #4
 
-; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
 declare i32 @llvm.umax.i32(i32, i32) #4
 
 attributes #0 = { allockind("alloc,zeroed") allocsize(0) "alloc-family"="runtime.alloc" "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
 attributes #1 = { "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
 attributes #2 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
-attributes #3 = { argmemonly nocallback nofree nounwind willreturn writeonly }
-attributes #4 = { nocallback nofree nosync nounwind readnone speculatable willreturn }
+attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
+attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
 attributes #5 = { nounwind }
diff --git a/compiler/testdata/zeromap.ll b/compiler/testdata/zeromap.ll
index 69932a1a..6a3d3132 100644
--- a/compiler/testdata/zeromap.ll
+++ b/compiler/testdata/zeromap.ll
@@ -43,14 +43,14 @@ entry:
   ret i32 %6
 }
 
-; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
 declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #4
 
 declare void @runtime.memzero(ptr, i32, ptr) #1
 
 declare i1 @runtime.hashmapBinaryGet(ptr dereferenceable_or_null(40), ptr, ptr, i32, ptr) #1
 
-; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
 declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #4
 
 ; Function Attrs: noinline nounwind
@@ -168,5 +168,5 @@ attributes #0 = { allockind("alloc,zeroed") allocsize(0) "alloc-family"="runtime
 attributes #1 = { "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
 attributes #2 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
 attributes #3 = { noinline nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
-attributes #4 = { argmemonly nocallback nofree nosync nounwind willreturn }
+attributes #4 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
 attributes #5 = { nounwind }
-- 
2.41.0